การวาดรูปภาพโดยใช้กราฟิก

การวาดรูปภาพโดยใช้กราฟิก

ด้วยไลบรารี Aspose.PSD คุณสามารถวาดรูปร่างพื้นฐาน เช่น เส้น, สี่เหลี่ยม และวงกลม รวมถึงรูปร่างที่ซับซ้อน เช่น รูปหลายเหลี่ยม, เส้นโค้ง, ภาพรูปวงกลมและรูปร่างเบเซียร์ ไลบรารี Aspose.PSD สร้างรูปร่างเหล่านี้โดยใช้คลาส Graphics ซึ่งตั้งอยู่ในเนมสเปซ Aspose.PSD ออบเจ็กต์ Graphics รับผิดชอบในการดำเนินการวาดต่าง ๆ บนภาพ, ซึ่งจะเปลี่ยนแปลงพื้นผิวของภาพ คลาส Graphics ใช้อ็อบเจกต์ช่วยเหลือหลายอย่างเพื่อเสริมสร้างรูปร่าง:

·         Pens, ใช้สำหรับวาดเส้น, ขอบรูปร่าง หรือเรนเดอร์ภาพเรขาคณิ.

·         Brushes, ใช้สำหรับกำหนดว่าพื้นที่จะถูกเติม.

·         Fonts, ใช้สำหรับกำหนดรูปร่างของตัวอักษรข้อความ.

การวาดด้วยคลาส Graphics

ด้านล่างนี้เป็นตัวอย่างโค้ดที่แสดงการใช้คลาส Graphics ตัวอย่างโค้ดมีการแบ่งเป็นส่วนต่าง ๆ เพื่อให้ง่ายต่อการติดตาม ขั้นตอนต่อไป เป็นตัวอย่างโชว์ว่าจะทำอย่างไร:

  1. สร้างภาพ.
  2. สร้างและกำหนดค่าให้กับอ็อบเจกต์ Graphics.
  3. เคลียร์พื้นผิว.
  4. วาดวงกลม.
  5. วาดรูปหลายเหลี่ยมและบันทึกภาพ.

ตัวอย่างการเขียนโปรแกรม

การสร้างภาพ

เริ่มต้นด้วยการสร้างภาพโดยใช้วิธีใดก็ได้ที่อธิบายไว้ในการสร้างไฟล์.

// 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 โดยเรียก Clear ของคลาส Graphics และส่งสีเป็นพารามิเตอร์ วิธีนี้จะเติมพื้นผิว 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 ได้เปิดเผยเมทอดหลายรูปแบบเพื่อวาดและเติมรูปร่าง คุณจะพบรายการเต็มของเมทอดทั้งหมดใน Aspose.PSD for .NET API Reference มีเวอร์ชันล่วงหน้าของเมทอด 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 เพื่อให้แน่ใจว่ามันถูกเก็บอย่างถูกต้อง.