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