การวาดรูปภาพ
การวาดเส้น
ตัวอย่างนี้ใช้คลาส Graphics เพื่อวาดรูปร่างเส้นบนผิวภาพ โดยในตัวอย่างจะสร้างรูปภาพใหม่และวาดเส้นบนผิวภาพโดยใช้วิธี DrawLine ที่เปิดเผยโดยคลาส Graphics ก่อนที่เราจะสร้าง PsdImage โดยระบุความสูงและความกว้างของมัน
เมื่อภาพถูกสร้างแล้ว เราจะใช้วิธี Clear ที่เปิดเผยโดยคลาส Graphics เพื่อตั้งค่าสีพื้นหลังของมัน วิธี DrawLine ของคลาส Graphics ถูกใช้เพื่อวาดเส้นบนภาพโดยเชื่อมโยงสองโครงสร้างจุด วิธีนี้มีหลายรูปแบบที่รับอาร์กิวเมนต์เป็นของคลาส Pen และคู่ของพอยนต์หรือโครงสร้าง Point/PointF เป็นอาร์กิวเมนต์ คลาส 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 โดยระบุความสูงและความกว้างของมัน
หลังจากสร้างภาพเราจะสร้างและกำหนดวัตถุคลาสกราฟิกและตั้งค่าสีพื้นหลังของภาพโดยใช้วิธี Clear ของคลาส Graphics วิธี DrawEllipse ของคลาส Graphics ถูกใช้เพื่อวาดรูปร่างวงรีบนผิวภาพที่ระบุโดยโครงสร้างสี่เหลี่ยมครอบคลุม วิธีนี้มีหลายรูปแบบที่รับอาร์กิวเมนต์เป็นอินสแตนซ์ของคลาส Pen และคลาส Rectangle/RectangleF หรือคู่ของพอยนและสู่จั้งความกว้างเป็นอาร์กิวเมนต์ คลาส Pen กำหนดอ็อบเจกต์ที่ใช้ในการวาดเส้น เส้นเรียกคลาสลูกเหลี่ยม และเก็บเซตของสี่เลขที่แทนตำแหน่งและขนาดของสี่เหลี่ยม คลาสสี่เหลี่ยมมีโค้ดสองแบบที่มีการเก็บ๊ค็องสตรักเจอร์วาดโครงสร้างสี่เหลี่ยมด้วยขนาดบคำสั่งและที่ตั้งที่ระบุ คลาส SolidBrush ใช้สำหรับการวาดต่อเนื่องด้วยสีที่ต้องการสุดท้ายภาพถูกส่งออกไปยังรูปแบบไฟล์ 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 หรือคู่ของพ้อยนแลจูเช่ความกว้างและความสูงเป็นอาร์กิวเมนต์ คลาสสี่เหลี่ยมเก็บเซ็ตของสี่เลขที่แทนตำแหน่งและขนาดของสี่เหลี่ยม คลาสสี่เหลี่ยมมีโค้ดสองแบบที่มีการสร้างโครงสร้างสี่เหลี่ยมด้วยขนาดและตำแหน่งที่ระบุ สุดท้ายภาพถูกส่งออกไปยังรูปแบบไฟล์ 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 แสดงส่วนหนึ่งของวงรีที่ระบุโดยโครงสร้างสี่เหลี่ยมหรือคู่จุด วิธีนี้มีหลายน็อเวอร์โหลดที่รับอินสแตนซ์ของคลาสเอ็นและโครงสร้าง 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 เพื่อวาดรูปร่างเบเซียร์บนผิวภาพ เพื่อสาธิตการทำงาน เราจะสร้าง PsdImage โดยระบุความสูงและความกว้างของมัน เมื่อภาพถูกสร้างแล้ว เราจะใช้วิธี Clear ที่เปิดเผยโดยคลาส Graphics เพื่อตั้งค่าสีพื้นหลังของมัน
วิธี DrawBezier ของคลาส Graphics ถูกใช้เพื่อวาดรูปร่างทางเบเซียร์บนผิวภาพที่กำหนดโดยโครงสร้างสี่โครงสร้างพอยต์ วิธีนี้มีหลายรูปแบบที่รับอินสแตนของคลาส Pen และสี่แพร์ของพอยต์ระเดียยระ ตอนๆ หรือสี่ส่วนสี่โครงสร้าง หรืออาเรย์ของสี่จุดโครงสร้าง คลาส 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 เป็นไลบรารี่ที่มีคุณลักษณะมากมายรวมถึงการสร้างรูปภาพจากศูนย์ เรียกใช้ความสามารถหลักเช่นการจัดการข้อมูลแบบบิทแมปของภาพ หรือใช้คุณสมบัติขั้นสูงเช่นกราฟิกและ GraphicsPath เพื่อวาดรูปร่างบนพื้นผิวภาพด้วยความช่วยเหลือของพูจรีและปานต่าง ๆ ใกล้ใช้คลาส RasterImage ของ Aspose.PSD คุณสามารถดึงข้อมูลพิกเซลของพื้นที่ภาพและจัดการด้วย คลาส RasterImage ประกอบด้วยความสามารถในการวาดหลักทั้งหมด เช่นการข้าถอดและตั้งค่าพิกเซลและเม