Manipulating PNG Images
تحديد الشفافية لصور PNG
أحد مزايا حفظ الصور بتنسيق PNG هو أن PNG يمكن أن يكون لها خلفية شفافة. Aspose.PSD for .NET يوفر ميزة تحديد الشفافية لصور PNG وصور الرسم النقطي كما هو موضح في القسم أدناه. يمكن استخدام واجهة برمجة التطبيقات Aspose.PSD for .NET لتعيين أي لون كشفاف أثناء إنشاء صور PNG جديدة أو تحويل الصور الحالية إلى تنسيق PNG. لهذه الأغراض، قامت واجهة برمجة التطبيقات Aspose.PSD for .NET بتعريض خاصية TransparentColor وتعريف PngColorType الذي يمكن تعيينه لتحديد أي لون ليتم عرضه على أنه شفاف في صورة PNG. يوضح مقتطف الكود الذي يتم توفيره أدناه كيفية تحويل صورة PSD حالية إلى صورة PNG عن طريق تحديد اللون المطلوب كشفافًا.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// specify the PNG image transparency options and save to file. | |
psdImage.TransparentColor = Color.White; | |
psdImage.HasTransparentColor = true; | |
PngOptions opt = new PngOptions(); | |
psdImage.Save(dataDir + "Specify_Transparency_result.png", new PngOptions()); | |
} |
تعيين الدقة لصور PNG
يكشف Aspose.PSD for .NET عن فئة ResolutionSetting التي يمكن استخدامها لتعيين الدقة لجميع تنسيقات الصور بما في ذلك PNG. يوضح هذا المقال كيفية استخدام واجهة برمجة التطبيقات Aspose.PSD for .NET لتعيين معلمات الدقة الأفقية والرأسية لتنسيق صور PNG. يقوم مقتطف الكود أدناه بتحميل صورة PSD حالية وتحويلها إلى تنسيق PNG مع تغيير الدقة.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the horizontal & vertical resolutions and Save the result on disc | |
PngOptions options = new PngOptions(); | |
options.ResolutionSettings = new ResolutionSetting(72, 96); | |
psdImage.Save(dataDir + "SettingResolution_output.png", options); | |
} |
ضغط ملفات PNG
الصورة النقطية المحمولة PNG هي تنسيق ضغط لا فقد في نقل البيانات النقطية عبر الشبكات. عند حفظ صورة كملف PNG في أي برنامج، قد يُطلب منك اختيار مستوى ضغط في نطاق من 0 إلى أي مستوى كحد أقصى. تقوم هذه القيمة بالضغط على حجم الملف ولا تقلل من جودة الصورة. يصف هذا المقال كيفية السماح لك بالتحكم في حجم ملفات PNG باستخدام واجهة برمجة التطبيقات Aspose.PSD. يمكن استخدام واجهة برمجة التطبيقات Aspose.PSD لتعيين مستويات الضغط لتنسيق ملف PNG باستخدام فئة PngOptions التي تحتوي على خاصية CompressionLevel من النوع int. تقبل هذه الخاصية قيمة من 0 إلى 9 حيث 9 هو أقصى درجة ضغط. يوضح مقتطف الكود الذي يتم توفيره أدناه كيفية تعيين مستويات الضغط باستخدام واجهة برمجة التطبيقات Aspose.PSD for .NET.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Loop over possible CompressionLevel range | |
for (int i = 0; i <= 9; i++) | |
{ | |
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk | |
PngOptions options = new PngOptions(); | |
options.CompressionLevel = i; | |
psdImage.Save(dataDir + i + "_out.png", options); | |
} | |
} |
تحديد عمق البت لصور PNG
عمق البت في التصوير هو عدد البتات المستخدمة للإشارة إلى لون واحد لبكسل مفرد في صورة نقطية. مثل جميع تنسيقات الصور النقطية الأخرى، يتم تمثيل عمق ألوان PNG أيضًا بالبتات مثل 1 بت (2 ألوان)، 2 بت (4 ألوان)، 4 بت (16 لون) و 8 بت (256 لون). يمكن استخدام واجهة برمجة التطبيقات Aspose.PSD for .NET لتعيين عمق البت لصور PNG باستخدام BitDepth الذي تقدمه فئة PngOptions. في الوقت الحالي، يمكن تعيين خاصية BitDepth إلى 1 أو 2 أو 4 أو 8 بتات لأنواع الألوان الرمادية والألوان المؤشرة. فقط يدعم 8 بتات لجميع أنواع الألوان الأخرى. يوضح مقتطف الكود الذي يتم توفيره أدناه كيفية تعيين عمق البت لصورة PNG.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the desired ColorType, BitDepth according to the specified ColorType and save image | |
PngOptions options = new PngOptions(); | |
options.ColorType = PngColorType.Grayscale; | |
options.BitDepth = 1; | |
psdImage.Save(dataDir + "SpecifyBitDepth_out.png", options); | |
} |
تطبيق طرق تصفية على صور PNG
عمق البت في التصوير هو عدد البتات المستخدمة للإشارة إلى لون واحد لبكسل مفرد في صورة نقطية. مثل جميع تنسيقات الصور النقطية الأخرى، يتم تمثيل عمق ألوان PNG أيضًا بالبتات مثل 1 بت (2 ألوان)، 2 بت (4 ألوان)، 4 بت (16 لون) و 8 بت (256 لون). يمكن استخدام واجهة برمجة التطبيقات Aspose.PSD for .NET لتعيين عمق البت لصور PNG باستخدام خاصية BitDepth التي تقدمها فئة PngOptions. في الوقت الحالي، يمكن تعيين خاصية BitDepth إلى 1 أو 2 أو 4 أو 8 بتات لأنواع الألوان الرمادية والألوان المؤشرة. فقط يدعم 8 بتات لجميع أنواع الألوان الأخرى. يوضح مقتطف الكود الذي يتم توفيره أدناه كيفية تعيين عمق البت لصورة PNG.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the PNG filter method and Save changes to the disc | |
PngOptions options = new PngOptions(); | |
options.FilterType = PngFilterType.Paeth; | |
psdImage.Save(dataDir + "ApplyFilterMethod_out.png", options); | |
} |
تغيير لون الخلفية لصورة PNG شفافة
تستطيع صور PNG تنسيق الصور PNG أن تحتوي على خلفية شفافة. يوفر Aspose.PSD for .NET ميزة تغير لون الخلفية لصورة PNG التي تحتوي على خلفية شفافة. يمكن استخدام واجهة برمجة التطبيقات Aspose.PSD for .NET لتعيين/تغيير لون صورة PNG شفافة. يوضح مقتطف الكود الذي يتم توفيره أدناه كيفية تعيين/تغيير لون خلفية صورة PNG شفافة.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
int[] pixels = psdImage.LoadArgb32Pixels(psdImage.Bounds); | |
// Iterate through the pixel array and Check the pixel information | |
//that if it is a transparent color pixel and Change the pixel color to white | |
int transparent = psdImage.TransparentColor.ToArgb(); | |
int replacementColor = Color.Yellow.ToArgb(); | |
for (int i = 0; i < pixels.Length; i++) | |
{ | |
if (pixels[i] == transparent) | |
{ | |
pixels[i] = replacementColor; | |
} | |
} | |
// Replace the pixel array into the image. | |
psdImage.SaveArgb32Pixels(psdImage.Bounds, pixels); | |
psdImage.Save(dataDir + "ChangeBackground_out.png", new PngOptions()); | |
} |