ציור תמונות באמצעות גרפיקה
ציור תמונות באמצעות גרפיקה
עם ספריית Aspose.PSD ניתן לצייר צורות פשוטות כמו קווים, מלבנים ומעגלים, וגם צורות מורכבות כמו מצולות, עקומות, קשתות וצורות Bezier. ספריית Aspose.PSD יוצרת צורות כאלו באמצעות מחלקת Graphics השוכנת במרחב השמות Aspose.PSD. אובייקטי גרפיקה אחראים לביצוע פעולות שונות של שרטוט על תמונה, משנים את משטח התמונה. מחלקת Graphics משתמשת במגוון אובייקטים עזר כדי לשפר את הצורות:
· עטים, לשרטוט קווים, להדגיש צורות או לעשות ייצוגים גיאומטריים אחרים.
· מכחול עם, להגדיר איך אזורים מתמלאים.
· גופנים, להגדיר את הצורה של תווים בטקסט.
ציור עם מחלקת הגרפיקה
להלן דוגמה לקוד המדגים את שימוש במחלקת Graphics. קוד המקור המופיע מחולק לחלקים מספרים כדי לשמר אותו פשוט וקל לעקוב אחריו. שלב אחר שלב, הדוגמאות מראות כיצד לבצע:
- יצירת תמונה.
- יצירה ואיתחול של אובייקט Graphics.
- ניקוי השטח.
- שרטוט אליפסה.
- שרטוט מצולה ממולאת ושמירת התמונה.
דוגמאות תכנות
יצירת תמונה
התחל על ידי יצירת תמונה באמצעות אחת מהשיטות המתוארות ביצירת קבצים.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string loadpath = dataDir + "sample.psd"; | |
string outpath = dataDir + "CoreDrawingFeatures.bmp"; | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(loadpath)) | |
{ | |
// load pixels | |
var 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()); | |
} |
יצירה ואיתחול אובייקט Graphics
לאחר מכן יצור ואיתחול של אובייקט Graphics על ידי מעבירה של התמונה לאובייקט הבונה שלו.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string outpath = dataDir + "Arc.bmp"; | |
// Create an instance of BmpOptions and set its various properties | |
BmpOptions saveOptions = new BmpOptions(); | |
saveOptions.BitsPerPixel = 32; | |
// Create an instance of Image | |
using (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.Yellow); | |
// 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.Black), 0, 0, width, height, startAngle, sweepAngle); | |
// export image to bmp file format. | |
image.Save(outpath, saveOptions); | |
} |
ניקוי השטח
נקה את שטח הגרפיקה על ידי קריאה לפעולת ניקוי של מחלקת Graphics והעברת צבע כפרמטר. פעולה זו ממלאה את שטח הגרפיקה עם הצבע הנתון כארגומנט.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(500, 500)) | |
{ | |
var graphics = new Graphics(image); | |
// Clear the image surface with white color and Create and initialize a Pen object with blue color | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Blue); | |
// 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)); | |
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f)) | |
{ | |
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) }); | |
} | |
// export modified image. | |
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions()); | |
} |
שרטוט אליפסה
תוכל ללמוד כי למחלקת Graphics חשפה רשימה ארוכה של שיטות לשרטוט ולמילוי צורות. תמצא את רשימת השיטות המלאה בתיעוד ה- API של Aspose.PSD for .NET. קיימות גרסאות רבות של השיטה DrawEllipse שנחשפות על ידי מחלקת Graphics. כל השיטות אלו מקבלות אובייקט Pen כארגומנט הראשון שלהן. הפרמטרים הבאים מאותגרים להגדיר את המלבן הגומי סביב האליפסה. בשם דוגמת זאת, השתמש בגרסה המקבלת אובייקט Rectangle כמקביל שני כדי לצייר אליפסה באמצעות אובייקט Pen בצבע שנבחר על ידך.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
string loadpath = dataDir + "sample.psd"; | |
string outpath = dataDir + "CoreDrawingFeatures.bmp"; | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(loadpath)) | |
{ | |
// load pixels | |
var 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()); | |
} |
שרטוט מצולה ממולאת
לאחר מכן, צייר מצולה באמצעות ה- LinearGradientBrush ומערך של נקודות. למחלקת Graphics חשפה כמה גרסאות עמודות של שיטת המילוי (FillPolygon). כל אחת מהן מקבלת אובייקט Brush כארגומנט הראשון, שמגדיר את המאפיינים של מילוי. הפרמטר השני הוא מערך של נקודות. נא לשים לב כי לכל שתי נקודות רצופות במערך נקבעת צד של המצולה.
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(500, 500)) | |
{ | |
var graphics = new Graphics(image); | |
// Clear the image surface with white color and Create and initialize a Pen object with blue color | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Blue); | |
// 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)); | |
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f)) | |
{ | |
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) }); | |
} | |
// export modified image. | |
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions()); | |
} |
ציור תמונות באמצעות גרפיקה: מקור מלא
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Create an instance of Image | |
using (PsdImage image = new PsdImage(500, 500)) | |
{ | |
var graphics = new Graphics(image); | |
// Clear the image surface with white color and Create and initialize a Pen object with blue color | |
graphics.Clear(Color.White); | |
var pen = new Pen(Color.Blue); | |
// 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)); | |
using (var linearGradientBrush = new LinearGradientBrush(image.Bounds, Color.Red, Color.White, 45f)) | |
{ | |
graphics.FillPolygon(linearGradientBrush, new[] { new Point(200, 200), new Point(400, 200), new Point(250, 350) }); | |
} | |
// export modified image. | |
image.Save(dataDir + "DrawingUsingGraphics_output.bmp", new BmpOptions()); | |
} |
כל המחלקות שמממשות IDisposable וגושים שאינם מנוהלים נפתחים בפקודת Using כדי לוודא שהם יתפשטו בצורה נכונה.