ציור תמונות באמצעות Graphics

ציור תמונות באמצעות Graphics

עם ספריית Aspose.PSD תוכל לצייר צורות פשוטות כמו קווים, מלבנים ומעגלים, כמו גם צורות מורכבות כמו מצולעים, עקופים, קשתות וצורות בזיר. ספריית Aspose.PSD יוצרת צורות כאלה באמצעות מחלקת Graphics השוכנת במרחב השמות של Aspose.PSD. עצם Graphics אחראי על ביצוע פעולות שונות של ציור על תמונה, כך שמשנים את משטח התמונה. מחלקת Graphics משתמשת בגרסאות של מופעי מחסנית שונים לשיפור הצורות:

  • עטים, לצייר קווים, להיפתר בצורות או לצייר ייצוגים גיאומטריים אחרים.
  • מברשות, להגדיר איך אזורים יומלאו.
  • גופנים, להגדיר את צורת התוים של טקסט.

ציור עם מחלקת Graphics

למטה דוגמה לקוד המדגים את שימוש במחלקת Graphics. קוד המקור של הדוגמה נחלק למספר חלקים כדי לשמור עלו פשוט ונוח למעקב אחריו. שלב אחר שלב, הדוגמאות מציגות כיצד ל:

  1. ליצור תמונה.
  2. ליצור ולאתחל אובייקט Graphics.
  3. לנקות את המשטח.
  4. לצייר אליפסה.
  5. לצייר מצולע ממולא ולשמור את התמונה.

דוגמאות תכנות

יצירת תמונה

התחל ביצירת תמונה בכל אחת מהשיטות הנתמכות המתוארות ביצירת קבצים.

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);
}

יצירה ואתחול אובייקט Graphics

לאחר מכן, צור ואתחל אובייקט Graphics על ידי מעבירת אובייקט Image לבונה.

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 על ידי קריאה לשיטת הניקוי של מחלקת Graphics והעברת צבע כפרמטר. שיטה זו ממלאה את משטח ה- Graphics בצבע שהועבר כארגומנט.

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.drawEllipse(pen, new Rectangle(10, 10, 150, 100));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

ציור אליפסה

ניתן להבחין שמחלקת Graphics חשפה כמות רבה של שיטות לצייר ולמלא צורות. תמצא את הרשימה המלאה של השיטות במדריך ה-API של Aspose.PSD ל- Java. ישנם מספר גרסאות מועמדות למחלקת DrawEllipse שמוצגות על ידי מחלקת Graphics. כל אלה מקבלות אובייקט Pen כארגומנט הראשון שלהן. הפרמטרים המאוחרים מועברים להגדרת המלבן מסביב לאליפסה. למען הדוגמה, השתמש בגרסה שמקבלת אובייקט Rectangle כפרמטר שני כדי לצייר אליפסה באמצעות אובייקט Pen בצבע הרצוי.

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);
}

ציור מצולע ממולא

באמצעות LinearGradientBrush ומערך של נקודות, ניתן לצייר מצולע. מחלקת Graphics חשפה מספר גרסאות מועמדות למחלקת FillPolygon. כל אלו מקבלות אובייקט Brush כארגומנט ראשון שלהן, המגדיר את התכונות של המילוי. הפרמטר השני הוא מערך של נקודות. יש לשים לב שכל שתי נקודות רצופות במערך מציינות צד של המצולע.

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.drawEllipse(pen, new Rectangle(10, 10, 150, 100));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

ציור תמונות באמצעות Graphics : מקור מלא

String dataDir = Utils.getDataDir(DrawingUsingGraphics.class) + "DrawingImages/";
// Create an instance of Image
try (PsdImage image = new PsdImage(500, 500)) {
Graphics graphics = new Graphics(image);
// Clear the image surface with white color and Create and initialize a Pen object with blue color
graphics.clear(Color.getWhite());
Pen pen = new Pen(Color.getBlue());
// Draw Ellipse by defining the bounding rectangle of width 150 and height 100 also Draw a polygon using the LinearGradientBrush
graphics.drawEllipse(pen, new Rectangle(10, 10, 150, 100));
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(image.getBounds(), Color.getRed(), Color.getWhite(), 45f);
// graphics.fillPolygon(linearGradientBrush, new Point(200, 200), new Point(400, 200), new Point(250, 350));
Point[] points = {new Point(200, 200), new Point(400, 200), new Point(250, 350)};
graphics.fillPolygon(linearGradientBrush, points);
// export modified image.
image.save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions());
}

כל המחלקות שמיישמות IDisposable ומגישון משאבים שאינם מנוהלים, מופעלות בהצהרת שימוש לסימן ייבוא מסודר שנשמר על ניתוקן בצורה נכונה.