使用Graphics绘制图像
使用Graphics绘制图像
使用Aspose.PSD库,您可以绘制简单的形状,如直线、矩形和圆,还可以绘制复杂的形状,如多边形、曲线、弧形和贝塞尔曲线。Aspose.PSD库使用位于Aspose.PSD命名空间中的Graphics类创建这些形状。Graphics对象负责在图像上执行不同的绘图操作,从而改变图像的表面。Graphics类使用各种辅助对象来增强这些形状:
- 画笔(Pens),用于绘制线条、轮廓形状或渲染其他几何表示。
- 画刷(Brushes),定义填充区域的方式。
- 字体(Fonts),定义文本字符的形状。
使用Graphics类绘图
以下是演示使用Graphics类的代码示例。示例源代码已分成几个部分,以保持简单易懂。逐步,示例展示如何:
- 创建一个图像。
- 创建并初始化一个Graphics对象。
- 清空表面。
- 绘制一个椭圆。
- 绘制一个填充的多边形并保存图像。
编程示例
创建一个图像
首先,使用创建文件中描述的任何方法创建一个图像。
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对象
然后,通过将Image对象传递给其构造函数来创建和初始化一个Graphics对象。
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类的Clear方法并传递一个颜色参数来清空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类公开了许多方法来绘制和填充形状。您会在Aspose.PSD for Java API参考中找到这些方法的完整列表。Graphics类公开了几个重载版本的DrawEllipse方法。所有这些方法的第一个参数都接受一个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接口和访问非托管资源的类都在Using语句中实例化,以确保它们被正确处理。