Vẽ Hình Ảnh Sử Dụng Graphics

Vẽ Hình Ảnh Sử Dụng Graphics

Với thư viện Aspose.PSD, bạn có thể vẽ các hình dạng đơn giản như đường thẳng, hình chữ nhật và hình tròn, cũng như các hình dạng phức tạp như đa giác, đường cong, cung và hình dạng Bézier. Thư viện Aspose.PSD tạo các hình dạng như vậy bằng cách sử dụng lớp Graphics nằm trong không gian tên Aspose.PSD. Đối tượng Graphics chịu trách nhiệm thực hiện các hoạt động vẽ khác nhau trên một hình ảnh, từ đó thay đổi bề mặt của hình ảnh. Lớp Graphics sử dụng một loạt các đối tượng trợ giúp để tăng cường các hình dạng:

  • Bút, để vẽ đường, đường viền hình dạng hoặc vẽ các biểu thị hình học khác.
  • Cọ, để xác định cách điền vào vùng.
  • Phông chữ, để xác định hình dạng của các ký tự văn bản.

Vẽ với Lớp Graphics

Dưới đây là một ví dụ mã nguồn demo về việc sử dụng lớp Graphics. Mã nguồn ví dụ đã được chia thành nhiều phần để giữ nó đơn giản và dễ theo dõi. Từng bước, ví dụ sẽ hướng dẫn cách:

  1. Tạo một hình ảnh.
  2. Tạo và khởi tạo một đối tượng Graphics.
  3. Xóa bề mặt.
  4. Vẽ một hình ellipse.
  5. Vẽ một đa giác đã tô màu và lưu hình ảnh.

Mẫu Lập Trình

Tạo Một Hình Ảnh

Bắt đầu bằng cách tạo một hình ảnh bằng cách sử dụng bất kỳ phương pháp nào được mô tả trong Viết Tệp.

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

Tạo và Khởi Tạo Một Đối Tượng Graphics

Tiếp theo, tạo và khởi tạo một đối tượng Graphics bằng cách truyền đối tượng Image vào hàm tạo nó.

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

Xóa Bề Mặt

Xóa bề mặt của Graphics bằng cách gọi phương thức Clear của lớp Graphics và truyền màu là tham số. Phương thức này sẽ lấp đầy bề mặt của Graphics bằng màu được truyền là đối số.

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

Vẽ Một Hình Ellipse

Bạn có thể thấy rằng lớp Graphics đã tiết lộ rất nhiều phương thức để vẽ và tô màu các hình dạng. Bạn sẽ tìm thấy danh sách đầy đủ các phương thức trong Aspose.PSD cho Tài Liệu Tham Khảo API Java. Có một số phiên bản nạp chồng của phương thức DrawEllipse được tiết lộ bởi lớp Graphics. Tất cả các phương thức này chấp nhận một đối tượng Pen là đối số đầu tiên. Các tham số sau đó được truyền để xác định hình chữ nhật bao quanh hình ellipse. Để mục đích của ví dụ này, hãy sử dụng phiên bản chấp nhận một đối tượng Rectangle làm tham số thứ hai để vẽ một hình ellipse bằng đối tượng Pen trong màu bạn muốn.

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

Vẽ Một Đa Giác Đã Tô Màu

Tiếp theo, vẽ một đa giác bằng cách sử dụng LinearGradientBrush và một mảng các điểm. Lớp Graphics đã tiết lộ một số phiên bản nạp chồng của phương thức FillPolygon. Tất cả những phương thức này chấp nhận một đối tượng Brush là đối số đầu tiên, xác định các đặc điểm của lớp fill. Tham số thứ hai là một mảng các điểm. Vui lòng lưu ý rằng mỗi hai điểm liên tiếp trong mảng xác định một cạnh của đa giác.

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

Vẽ Hình Ảnh Sử Dụng Graphics : Mã Nguồn Đầy Đủ

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

Tất cả các lớp thực hiện IDisposable và truy cập vào tài nguyên không quản lý được khởi tạo trong một câu lệnh Using để đảm bảo rằng chúng được loại bỏ đúng cách.