إنشاء، فتح، وحفظ الصور

إنشاء ملفات الصور

يسمح Aspose.PSD for .NET للمطورين بإنشاء صورهم الخاصة. استخدم الطريقة الثابتة Create المكشوفة بواسطة فئة الصورة لإنشاء صور جديدة. كل ما عليك فعله هو توفير كائن مناسب من إحدى فئات فضاء ImageOptions لتنسيق الصورة المطلوبة. لإنشاء ملف صورة، ابدأ أولاً بإنشاء مثيل من إحدى فئات فضاء ImageOptions. تحدد هذه الفئات تنسيق الصورة الناتجة. فيما يلي بعض الفئات من فضاء ImageOptions (لاحظ أن عائلة تنسيق ملفات PSD هي الوحيدة المدعومة حاليًا للإنشاء):

PsdOptions تحدد الخيارات لإنشاء ملف PSD. يمكن إنشاء ملفات الصور عن طريق تعيين مسار إخراج أو عن طريق تعيين تيار.

الإنشاء عن طريق تعيين المسار

أنشئ PsdOptions من ImageOptions فضاء ImageOptions وقم بتعيين الخصائص المختلفة. أهم خاصية لتعيينها هي الخاصية Source. تحدد هذه الخاصية أين تتواجد بيانات الصورة (في ملف أو تيار). في المثال أدناه، المصدر هو ملف. بعد تعيين الخصائص، قم بتمرير الكائن إلى إحدى الطرق الثابتة Create جنبًا إلى جنب مع معلمات العرض والارتفاع. يُعرف العرض والارتفاع بالبكسل.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Creates an instance of PsdOptions and set its various properties
PsdOptions psdOptions = new PsdOptions();
psdOptions.CompressionMethod = CompressionMethod.RLE;
// Define the source property for the instance of PsdOptions. Second boolean parameter determines if the file is temporal or not
psdOptions.Source = new FileCreateSource(desName, false);
// Creates an instance of Image and call Create method by passing the PsdOptions object
using (Image image = Image.Create(psdOptions, 500, 500))
{
image.Save();
}

الإنشاء باستخدام تيار

العملية لإنشاء صورة باستخدام تيار مماثلة لاستخدام المسار. الفرق الوحيد هو أنه يجب عليك إنشاء مثيل من StreamSource عن طريق تمرير كائن Stream إلى بناؤه وتعيينه لخاصية Source.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string desName = dataDir + "CreatingImageUsingStream_out.bmp";
// Creates an instance of BmpOptions and set its various properties
BmpOptions ImageOptions = new BmpOptions();
ImageOptions.BitsPerPixel = 24;
// Create an instance of System.IO.Stream
Stream stream = new FileStream(dataDir + "sample_out.bmp", FileMode.Create);
// Define the source property for the instance of BmpOptions Second boolean parameter determines if the Stream is disposed once get out of scope
ImageOptions.Source = new StreamSource(stream, true);
// Creates an instance of Image and call Create method by passing the BmpOptions object
using (Image image = Image.Create(ImageOptions, 500, 500))
{
// Do some image processing
image.Save(desName);
}

فتح ملفات الصور

يمكن للمطورين استخدام Aspose.PSD for .NET API لفتح ملفات الصور PSD الحالية لأغراض مختلفة، مثل إضافة تأثيرات على الصورة أو تحويل ملف موجود إلى تنسيق آخر. بغض النظر عن الغرض، يوفر Aspose.PSD طريقتين قياسيتين لفتح الملفات الحالية: من ملف أو من تيار.

الفتح من القرص

افتح ملف صورة عن طريق تمرير المسار واسم الملف كمعلمة إلى الطريقة الثابتة Load المكشوفة بواسطة فئة الصورة.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
psdImage.Save(destName, new PngOptions());
}

الفتح باستخدام تيار

أحيانًا، الصورة التي نحتاج إلى فتحها مخزنة كتيار. في مثل هذه الحالات، استخدم الإصدار المكدس من الطريقة Load. يقبل هذا الإصدار كائن Stream كوسيط لفتح الصورة.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
FileStream fStream = new FileStream(sourceFile, FileMode.Open);
fStream.Position = 0;
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(fStream))
{
PsdImage psdImage = (PsdImage)image;
MemoryStream stream = new MemoryStream();
psdImage.Save(stream, new PngOptions());
}

تحميل الصورة كطبقة

توضح هذه المقالة استخدام Aspose.PSD لتحميل صورة كطبقة. لقد قامت APIs للتلاعب بالصور Aspose.PSD بتوفير طرق فعالة وسهلة الاستخدام لتحقيق هذا الهدف. قد قامت Aspose.PSD بتعريض طريقة AddLayer في فئة PsdImage لإضافة صورة إلى ملف PSD كطبقة.

خطوات تحميل صورة إلى PSD كطبقة بسيطة كما يلي:

  • أنشئ مثيل صورة باستخدام فئة PsdImage بعرض وارتفاع محدد.
  • قم بتحميل ملف PSD كصورة باستخدام الطريقة الثابتة Load التي تعرضها فئة Image.
  • أنشئ مثيلًا من الفئة Layer وقم بتعيين طبقة صورة PSD إليها.
  • أضف الطبقة التي تم إنشاؤها باستخدام طريقة AddLayer التي تكشف عنها فئة PsdImage.
  • احفظ النتائج.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string filePath = dataDir + "PsdExample.psd";
string outputFilePath = dataDir + "PsdResult.psd";
using (var image = new PsdImage(200, 200))
{
using (var im = Image.Load(filePath))
{
Layer layer = null;
try
{
layer = new Layer((RasterImage)im);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw;
}
}
image.Save(outputFilePath);
}

تحميل الصورة كطبقة من تيار

توضح هذه المقالة استخدام Aspose.PSD لتحميل صورة كطبقة من تيار. لتحميل صورة من تيار، مرر ببساطة كائن تيار يحتوي على الصورة إلى بناء الفئة Layer. أضف الطبقة التي تم إنشاؤها باستخدام طريقة AddLayer التي تعرضها فئة PsdImage واحفظ النتائج.

وفيما يلي الشفرة المصدرية العينية.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string outputFilePath = dataDir + "PsdResult.psd";
var filesList = new string[]
{
"PsdExample.psd",
"BmpExample.bmp",
"GifExample.gif",
"Jpeg2000Example.jpf",
"JpegExample.jpg",
"PngExample.png",
"TiffExample.tif",
};
using (var image = new PsdImage(200, 200))
{
foreach (var fileName in filesList)
{
string filePath = dataDir + fileName;
using (var stream = new FileStream(filePath, FileMode.Open))
{
Layer layer = null;
try
{
layer = new Layer(stream);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw e;
}
}
}
image.Save(outputFilePath);
}

حفظ ملفات الصور

Aspose.PSD تتيح لك إنشاء ملفات الصور من البداية. كما توفر وسائل لتحرير ملفات الصور الموجودة. بمجرد إنشاء الصورة أو تعديلها، يتم عادة حفظ الملف على القرص. Aspose.PSD توفر لك طرقًا لحفظ الصور على القرص عن طريق تحديد مسار أو باستخدام كائن تيار.

الحفظ على القرص

تمثل فئة Image كائن صورة، لذا توفر هذه الفئة جميع الأدوات اللازمة لإنشاء وتحميل وحفظ ملف صورة. استخدم طريقة الحفظ Save لفئة Image لحفظ الصور. إحدى الإصدارات المكدسة لطريقة الحفظ تقبل موقع الملف كسلسلة.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
psdImage.Save(destName, new PngOptions());
}

الحفظ على تيار

يقبل إصدار آخر مكدس من طريقة الحفظ كائن تيار كوسيط ويحفظ ملف الصورة على التيار.

إذا تم إنشاء الصورة عن طريق تحديد أي من CreateOptions في بناء Image، يتم حفظ الصورة تلقائيًا على المسار أو التيار المقدم أثناء تهيئة فئة Image بالاستدعاء Save لطريقة التي لا تقبل أي معلمة.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
MemoryStream stream = new MemoryStream();
psdImage.Save(stream, new PngOptions());
}

ضبط لاستبدال الخطوط المفقودة

يمكن للمطورين استخدام Aspose.PSD for .NET API لتحميل ملفات الصور PSD الموجودة لأغراض مختلفة، على سبيل المثال، لتعيين اسم خط الطباعة الافتراضي عند حفظ مستندات PSD كصورة نقطية (في تنسيق PNG، JPG، و BMP). يجب استخدام هذا الخط الافتراضي كاستبدال لكافة الخطوط المفقودة (الخطوط التي لم يتم العثور عليها في نظام التشغيل الحالي). بمجرد تعديل الصورة، سيتم حفظ الملف على القرص.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFileName = "sample_konstanting.psd";
string[] outputs = new string[]
{
"replacedfont0.tiff",
"replacedfont1.png",
"replacedfont2.jpg"
};
using (PsdImage image = (PsdImage)Image.Load(sourceFileName, new PsdLoadOptions()))
{
// This way you can use different fonts for different outputs
image.Save(outputs[0], new TiffOptions(TiffExpectedFormat.TiffJpegRgb) { DefaultReplacementFont = "Arial" });
image.Save(outputs[1], new PngOptions { DefaultReplacementFont = "Verdana" });
image.Save(outputs[2], new JpegOptions { DefaultReplacementFont = "Times New Roman" });
}