绘制图像

绘制线条

此示例使用Graphics类在图像表面绘制线条形状。为了演示操作,示例创建了一个新的图像,并使用Graphics类公开的DrawLine方法在图像表面绘制线条。首先,我们将创建一个指定高度和宽度的PsdImage。

创建图像后,我们将使用Graphics类公开的Clear方法设置其背景颜色。Graphics类的DrawLine方法用于在连接两个点结构的图像上绘制一条线。该方法有多个重载,接受Pen类的实例和点或Point/PointF结构的坐标对作为参数。Pen类定义用于绘制线条、曲线和图形的对象。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类在图像表面上绘制椭圆形状。为了演示操作,示例创建了一个新的图像,并使用Graphics类公开的DrawEllipse方法在图像表面上绘制椭圆形状。首先,我们将创建一个指定高度和宽度的PsdImage。

创建图像后,我们将创建和初始化Graphics类对象,并使用Graphics类的Clear方法设置图像的背景颜色。Graphics类的DrawEllipse方法用于在由边界矩形结构指定的图像表面上绘制椭圆形状。该方法有多个重载,接受Pen和Rectangle/RectangleF类的实例或一对坐标、高度和宽度作为参数。Pen类定义用于绘制线条、曲线和图形的对象。Pen类有几个重载的构造函数,可绘制具有指定颜色、宽度和画笔的线条。Rectangle类存储表示矩形的位置和大小的四个整数。Rectangle类有几个重载的构造函数,可绘制具有指定大小和位置的矩形结构。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);
}

绘制矩形

在这个示例中,我们将在图像表面上绘制矩形形状。为了演示操作,示例创建了一个新的图像,并使用Graphics类公开的DrawRectangle方法在图像表面上绘制矩形形状。首先,我们将创建一个指定高度和宽度的PsdImage。然后,我们将使用Graphics类的Clear方法设置图像的背景颜色。

Graphics类的DrawRectangle方法用于在由矩形结构指定的图像表面上绘制矩形形状。该方法有多个重载,接受Pen和Rectangle/RectangleF类的实例或坐标对、宽度和高度作为参数。Rectangle类存储表示矩形的位置和大小的四个整数。Rectangle类有几个重载的构造函数,可绘制具有指定大小和位置的矩形结构。最后,将图像导出为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);
}

绘制弧

在这个绘制形状系列的会话中,我们将在图像表面上绘制弧形状。我们将使用Graphics的DrawArc方法在BMP图像上展示操作。首先,我们将创建一个指定高度和宽度的PsdImage。创建图像后,我们将使用Graphics类公开的Clear方法设置其背景颜色。

Graphics类的DrawArc方法用于在图像表面上绘制弧形状。DrawArc表示由矩形结构或坐标对指定的椭圆的一部分。该方法有多个重载,接受Pen类和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);
}

绘制Bezier曲线

此示例使用Graphics类在图像表面上绘制Bezier曲线形状。为了演示操作,示例创建了一个新的图像,并使用Graphics类公开的DrawBezier方法在图像表面上绘制Bezier曲线形状。首先,我们将创建一个指定高度和宽度的PsdImage。创建图像后,我们将使用Graphics类公开的Clear方法设置其背景颜色。

Graphics类的DrawBezier方法用于在由四个Point结构定义的图像表面上绘制Bezier样条形状。该方法有多个重载,接受Pen类的实例和四个有序的坐标对、四个Point/PointF结构或Point/PointF结构数组作为参数。Pen类定义用于绘制线条、曲线和图形的对象。Pen类有几个重载的构造函数,可绘制具有指定颜色、宽度和画笔的线条。最后,将图像导出为BMP文件格式。以下代码片段显示了如何在图像表面上绘制Bezier曲线形状。

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是一个库,提供许多有价值的功能,包括从头开始创建图像。使用核心功能进行绘图,例如操纵图像的位图信息,或者使用高级功能,如Graphics和GraphicsPath,在图像表面上绘制形状,借助不同的画刷和笔。使用Aspose.PSD的RasterImage类,您可以检索图像区域的像素信息并进行操作。RasterImage类包含整个核心绘图功能,例如获取和设置像素以及其他用于图像操作的方法。使用描述中创建文件的任一方法创建新图像,并将其分配给RasterImage类的实例。使用RasterImage类的LoadPixels方法检索图像部分的像素信息。一旦有像素数组,您可以通过例如更改每个像素的颜色来操纵它。在操纵像素信息后,使用RasterImage类的SavePixels方法将其设置回图像中的所需区域。以下代码片段显示了如何使用核心功能绘制图像。

String dataDir = Utils.getDataDir(CoreDrawingFeatures.class) + "DrawingImages/";
// Create an instance of BmpOptions and set its various properties
String loadpath = dataDir + "sample.psd";
String outpath = dataDir + "CoreDrawingFeatures.bmp";
// Create an instance of Image
try (PsdImage image = new PsdImage(loadpath)) {
// load pixels
int[] 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());
}