رسم الصور
رسم الخطوط
تستخدم هذا المثال فئة Graphics لرسم أشكال الخطوط على سطح الصورة. لتوضيح العملية ، ينشئ المثال صورة جديدة ويقوم برسم خطوط على سطح الصورة باستخدام الطريقة DrawLine التي تقدمها فئة Graphics. أولاً ، سنقوم بإنشاء PsdImage محددًا ارتفاعه وعرضه.
بمجرد إنشاء الصورة ، سنستخدم الطريقة Clear التي تقدمها فئة Graphics لتعيين لون الخلفية الخاص بها. تُستخدم طريقة DrawLine في فئة Graphics لرسم خط على صورة تربط نقطتين. تحتوي هذه الطريقة على العديد من التحميلات التي تقبل مثيلًا لفئة Pen وأزواجًا من تنسيقات النقاط أو Point/PointF كمعلمات. تعرف فئة Pen على كائن يُستخدم لرسم الخطوط والمنحنيات والأشكال. تحوي فئة Pen على العديد من المنشئات المحملة لرسم الخطوط باللون والعرض والفرشاة المحددين. تستخدم فئة SolidBrush لرسم بشكل مستمر بلون معين. أخيرًا يتم تصدير الصورة إلى تنسيق ملف BMP. يوضح مقتطف الكود التالي كيفية رسم أشكال الخطوط على سطح الصورة.
String dataDir = Utils.getDataDir(DrawingLines.class) + "DrawingImages/"; | |
// Create an instance of BmpOptions and set its various properties | |
String outpath = dataDir + "Lines.bmp"; | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.setBitsPerPixel(32); | |
// Create an instance of Image | |
try (Image image = new PsdImage(100, 100)) { | |
// Create and initialize an instance of Graphics class and Clear Graphics surface | |
Graphics graphic = new Graphics(image); | |
graphic.clear(Color.getYellow()); | |
// Draw two dotted diagonal lines by specifying the Pen object having blue color and co-ordinate Points | |
graphic.drawLine(new Pen(Color.getBlue()), 9, 9, 90, 90); | |
graphic.drawLine(new Pen(Color.getBlue()), 9, 90, 90, 9); | |
// Draw a four continuous line by specifying the Pen object having Solid Brush with red color and two point structures | |
graphic.drawLine(new Pen(new SolidBrush(Color.getRed())), new Point(9, 9), new Point(9, 90)); | |
graphic.drawLine(new Pen(new SolidBrush(Color.getAqua())), new Point(9, 90), new Point(90, 90)); | |
graphic.drawLine(new Pen(new SolidBrush(Color.getBlack())), new Point(90, 90), new Point(90, 9)); | |
graphic.drawLine(new Pen(new SolidBrush(Color.getWhite())), new Point(90, 9), new Point(9, 9)); | |
image.save(outpath, saveOptions); | |
} |
رسم البيضاوي
مثال رسم البيضاوي هو المقال الثاني في سلسلة رسم الأشكال. سنستخدم فئة Graphics لرسم شكل البيضاوي على سطح الصورة. لتوضيح العملية ، ينشئ المثال صورة جديدة ويقوم برسم شكل البيضاوي على سطح الصورة باستخدام طريقة DrawEllipse التي تقدمها فئة Graphics. أولاً ، سنقوم بإنشاء PsdImage محددًا ارتفاعه وعرضه.
بعد إنشاء الصورة ، سننشئ ونبدأ في تهيئة كائن فئة Graphics ونعين لون خلفية الصورة باستخدام الطريقة Clear في فئة Graphics. تُستخدم طريقة DrawEllipse في فئة Graphics لرسم شكل البيضاوي على سطح الصورة المحدد بتنسيق المستطيل الحدودي. تحوي هذه الطريقة على العديد من التحميلات التي تقبل مثيلًا لفئة Pen وـ Rectangle/RectangleF أو زوج من الإحداثيات، وارتفاع، وعرض كمعلمات. تعرف فئة Pen كائن تُستخدم لرسم الخطوط والمنحنيات والأشكال. تحوي فئة Pen على العديد من المنشئات المحملة لرسم الخطوط باللون والعرض والفرشاة المحددين. تقوم فئة Rectangle بتخزين مجموعة من أربعة أعداد صحيحة تمثل موقع وحجم مستطيل. تحوي فئة Rectangle على العديد من المنشئات المحملة لرسم هيكل المستطيل بالحجم والموقع المحددين. أخيرًا يتم تصدير الصورة إلى تنسيق ملف BMP. يوضح مقتطف الكود التالي كيفية رسم شكل البيضاوي على سطح الصورة.
String dataDir = Utils.getDataDir(DrawingEllipse.class) + "DrawingImages/"; | |
// Create an instance of BmpOptions and set its various properties | |
String outpath = dataDir + "Ellipse.bmp"; | |
// Create an instance of BmpOptions and set its various properties | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.setBitsPerPixel(32); | |
// Create an instance of Image | |
try (Image image = new PsdImage(100, 100)) { | |
// Create and initialize an instance of Graphics class and Clear Graphics surface | |
Graphics graphic = new Graphics(image); | |
graphic.clear(Color.getYellow()); | |
// Draw a dotted ellipse shape by specifying the Pen object having red color and a surrounding Rectangle | |
graphic.drawEllipse(new Pen(Color.getRed()), new Rectangle(30, 10, 40, 80)); | |
// Draw a continuous ellipse shape by specifying the Pen object having solid brush with blue color and a surrounding Rectangle | |
graphic.drawEllipse(new Pen(new SolidBrush(Color.getBlue())), new Rectangle(10, 30, 80, 40)); | |
// export image to bmp file format. | |
image.save(outpath, saveOptions); | |
} |
رسم المستطيل
في هذا المثال سنقوم برسم شكل المستطيل على سطح الصورة. لتوضيح العملية ، ينشئ المثال صورة جديدة ويقوم برسم شكل المستطيل على سطح الصورة باستخدام طريقة DrawRectangle التي تقدمها فئة Graphics. أولاً ، سنقوم بإنشاء PsdImage محددًا ارتفاعه وعرضه. ثم سنقوم بتعيين لون خلفية الصورة باستخدام الطريقة Clear في فئة Graphics.
تُستخدم طريقة DrawRectangle في فئة Graphics لرسم شكل المستطيل على سطح الصورة المحدد بتنسيق المستطيل. تحوي هذه الطريقة على العديد من التحميلات التي تقبل مثيلًا لفئة Pen وـ Rectangle/RectangleF أو زوج من الإحداثيات، وارتفاع، وعرض كمعلمات. تحتوي فئة Rectangle على مجموعة من أربعة أعداد صحيحة تمثل موقع وحجم مستطيل. تحوي فئة Rectangle على العديد من المنشئات المحملة لرسم هيكل المستطيل بالحجم والموقع المحددين. أخيرًا يتم تصدير الصورة إلى تنسيق ملف BMP. يوضح مقتطف الكود التالي كيفية رسم شكل المستطيل على سطح الصورة.
// Create an instance of BmpOptions and set its various properties | |
String outpath = dataDir + "Rectangle.bmp"; | |
// Create an instance of BmpOptions and set its various properties | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.setBitsPerPixel(32); | |
// Create an instance of Image | |
try (Image image = new PsdImage(100, 100)) { | |
// Create and initialize an instance of Graphics class, Clear Graphics surface, Draw a rectangle shapes and save all changes. | |
Graphics graphic = new Graphics(image); | |
graphic.clear(Color.getYellow()); | |
graphic.drawRectangle(new Pen(Color.getRed()), new Rectangle(30, 10, 40, 80)); | |
graphic.drawRectangle(new Pen(new SolidBrush(Color.getBlue())), new Rectangle(10, 30, 80, 40)); | |
// export image to bmp file format. | |
image.save(outpath, saveOptions); | |
} |
رسم القوس
في هذا الجزء من سلسلة رسم الأشكال ، سنقوم برسم شكل القوس على سطح الصورة. سنستخدم طريقة DrawArc في فئة Graphics لتوضيح العملية على صورة BMP. أولاً ، سنقوم بإنشاء PsdImage محددًا ارتفاعه وعرضه. بمجرد إنشاء الصورة ، سنستخدم الطريقة Clear التي تقدمها فئة Graphics لتعيين لون خلفيتها.
تُستخدم طريقة DrawArc في فئة Graphics لرسم شكل القوس على سطح الصورة. يمثل DrawArc جزءًا من بيضوي يُحدد بتنسيق المستطيل أو زوج الإحداثيات. تحوي هذه الطريقة على العديد من التحميلات التي تقبل مثيلًا لفئة Pen وهياكل Rectangle / RectangleF أو زوج من الإحداثيات، وعرض، وارتفاع كمعلمات. أخيرًا يتم تصدير الصورة إلى تنسيق ملف BMP. يوضح مقتطف الكود التالي كيفية رسم شكل القوس على سطح الصورة.
String dataDir = Utils.getDataDir(DrawingArc.class) + "DrawingImages/"; | |
// Create an instance of BmpOptions and set its various properties | |
String outpath = dataDir + "Arc.bmp"; | |
// Create an instance of BmpOptions and set its various properties | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.setBitsPerPixel(32); | |
// Create an instance of Image | |
try (Image image = new PsdImage(100, 100)) { | |
// Create and initialize an instance of Graphics class and clear Graphics surface | |
Graphics graphic = new Graphics(image); | |
graphic.clear(Color.getYellow()); | |
// Draw an arc shape by specifying the Pen object having red black color and coordinates, height, width, start & end angles | |
int width = 100; | |
int height = 200; | |
int startAngle = 45; | |
int sweepAngle = 270; | |
// Draw arc to screen and save all changes. | |
graphic.drawArc(new Pen(Color.getBlack()), 0, 0, width, height, startAngle, sweepAngle); | |
// export image to bmp file format. | |
image.save(outpath, saveOptions); | |
} |
رسم الخطوط المنحنية
يستخدم هذا المثال فئة Graphics لرسم شكل الخطوط المنحنية على سطح الصورة. لتوضيح العملية ، ينشئ المثال صورة جديدة ويقوم برسم شكل الخطوط المنحنية على سطح الصورة باستخدام طريقة DrawBezier التي تقدمها فئة Graphics. أولاً ، سنقوم بإنشاء PsdImage محددًا ارتفاعه وعرضه. بمجرد إنشاء الصورة ، سنستخدم الطريقة Clear التي تقدمها فئة Graphics لتعيين لون خلفية الصورة.
تُستخدم طريقة DrawBezier في فئة Graphics لرسم شكل الخطوط المنحنية على سطح الصورة المحدد بواسطة أربع هياكل نقطية. تحوي هذه الطريقة على العديد من التحميلات التي تقبل مثيلًا لفئة Pen وأربعة أزواج مرتبة من الإحداثيات أو أربعة هياكل Point/PointF أو مصفوفة من هياكل Point/PointF. تعرف فئة Pen كائنًا يُستخدم لرسم الخطوط والمنحنيات والأشكال. تحوي فئة Pen على العديد من المنشئات المحملة لرسم الخطوط باللون والعرض والفرشاة المحددين. أخيرًا يتم تصدير الصورة إلى تنسيق ملف BMP. يوضح مقتطف الكود التالي كيفية رسم الخطوط المنحنية على سطح الصورة.
String dataDir = Utils.getDataDir(DrawingBezier.class) + "DrawingImages/"; | |
// Create an instance of BmpOptions and set its various properties | |
String outpath = dataDir + "Bezier.bmp"; | |
// Create an instance of BmpOptions and set its various properties | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.setBitsPerPixel(32); | |
// Create an instance of Image | |
try (Image image = new PsdImage(100, 100)) { | |
// Create and initialize an instance of Graphics class and clear Graphics surface | |
Graphics graphic = new Graphics(image); | |
graphic.clear(Color.getYellow()); | |
// Initializes the instance of PEN class with black color and width | |
Pen BlackPen = new Pen(Color.getBlack(), 3); | |
float startX = 10; | |
float startY = 25; | |
float controlX1 = 20; | |
float controlY1 = 5; | |
float controlX2 = 55; | |
float controlY2 = 10; | |
float endX = 90; | |
float endY = 25; | |
// Draw a Bezier shape by specifying the Pen object having black color and co-ordinate Points and save all changes. | |
graphic.drawBezier(BlackPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY); | |
// export image to bmp file format. | |
image.save(outpath, saveOptions); | |
} |
رسم الصور باستخدام الوظائف الأساسية
Aspose.PSD هي مكتبة تقدم العديد من الميزات القيمة بما في ذلك إنشاء الصور من البداية. رسم باستخدام الوظائف الأساسية مثل تلاعب بمعلومات بتمبلت الصورة ، أو استخدام الميزات المتقدمة مثل Graphics و GraphicsPath لرسم الأشكال على سطح الصورة بمساعدة فرشاة وأقلام مختلفة. باستخدام فئة RasterImage في Aspose.PSD ، يمكنك الحصول على معلومات بيكسل منطقة الصورة وتلاعبها. تحتوي فئة RasterImage على معظم وظائف الرسم الأساسية ، مثل الحصول على بكسل وتعيينها وأساليب أخرى لتلاعب الصورة. قم بإنشاء صورة جديدة باستخدام أي من الطرق الموصوفة في إنشاء الملفات وتعيينها لنسخة من فئة RasterImage. استخدم طريقة LoadPixels في فئة RasterImage لاسترداد معلومات البكسل لجزء من الصورة. بمجرد الحصول على مصفوفة بكسل ، يمكنك التلاعب بها عن طريق تغيير لون كل بكسل على سبيل المثال. بعد تلاعب معلومات البكسل ، ضعها مرة أخرى في المنطقة المطلوبة في الصورة باستخدام طريقة SavePixels في فئة RasterImage. يوضح مقتطف الكود التالي كيفية رسم الصور باستخدام الوظائف الأساسية.
String dataDir = Utils.getDataDir(CoreDrawingFeatures.class) + "DrawingImages/"; | |
// Create an instance of BmpOptions and set its various properties | |
String loadpath = dataDir + "sample.psd"; | |
String outpath = dataDir + "CoreDrawingFeatures.bmp"; | |
// Create an instance of Image | |
try (PsdImage image = new PsdImage(loadpath)) { | |
// load pixels | |
int[] pixels = image.loadArgb32Pixels(new Rectangle(0, 0, 100, 10)); | |
for (int i = 0; i < pixels.length; i++) { | |
// specify pixel color value (gradient in this case). | |
pixels[i] = i; | |
} | |
// save modified pixels. | |
image.saveArgb32Pixels(new Rectangle(0, 0, 100, 10), pixels); | |
// export image to bmp file format. | |
image.save(outpath, new BmpOptions()); | |
} |