How to draw an image

How to draw an image

         There are several Aspose.Imaging library methods to draw graphics primitives such as DrawLine, DrawEllipse, DrawRectangle, DrawArc, DrawBezier and DrawString. Before drawing a first element, you need to create a graphic canvas to draw an image from scratch and define its properties. We create a surface of 100 x 100 pixels with a depth of 32 bits per pixel and use a yellow color background by utilizing the Clear method. To define a drawing tool, we will use an object Pen with a Blue color, to draw a dotted line or use SolidBrush for continuous lines, followed by coordinates of the start and end points of the lines:

// For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java
com.aspose.imaging.imageoptions.BmpOptions bmpCreateOptions = new com.aspose.imaging.imageoptions.BmpOptions();
bmpCreateOptions.setBitsPerPixel(32);
// Define the source property for the instance of BmpOptions
bmpCreateOptions.setSource(
new com.aspose.imaging.sources.StreamSource(new java.io.ByteArrayInputStream(new byte[100 * 100 * 4])));
// Creates an instance of Image and call create method by passing the
// bmpCreateOptions object
com.aspose.imaging.Image image = com.aspose.imaging.Image.create(bmpCreateOptions, 100, 100);
// Create and initialize an instance of Graphics class
com.aspose.imaging.Graphics graphic = new com.aspose.imaging.Graphics(image);
// Clear the image surface with Yellow color
graphic.clear(com.aspose.imaging.Color.getYellow());
// Draw a dotted line by specifying the Pen object having blue color and
// co-ordinate Points
graphic.drawLine(new Pen(com.aspose.imaging.Color.getBlue()), 9, 9, 90, 90);
graphic.drawLine(new Pen(com.aspose.imaging.Color.getBlue()), 9, 90, 90, 9);
// Draw a continuous line by specifying the Pen object having Solid
// Brush with red color and two point structures
graphic.drawLine(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getRed())),
new com.aspose.imaging.Point(9, 9), new com.aspose.imaging.Point(9, 90));
// Draw a continuous line by specifying the Pen object having Solid
// Brush with aqua color and two point structures
graphic.drawLine(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getAqua())),
new com.aspose.imaging.Point(9, 90), new com.aspose.imaging.Point(90, 90));
// Draw a continuous line by specifying the Pen object having Solid
// Brush with black color and two point structures
graphic.drawLine(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getBlack())),
new com.aspose.imaging.Point(90, 90), new com.aspose.imaging.Point(90, 9));
// Draw a continuous line by specifying the Pen object having Solid
// Brush with white color and two point structures
graphic.drawLine(new Pen(new com.aspose.imaging.brushes.SolidBrush(com.aspose.imaging.Color.getWhite())),
new com.aspose.imaging.Point(90, 9), new com.aspose.imaging.Point(9, 9));
// Save all changes.
image.save(dataDir + "DrawingLines_out.bmp");

         Please find more examples of drawing lines, ellipses, rectangles, arcs, Bezier curves and text strings in the Aspose drawing images guide.

The image example below demonstrates lines, ellipses and rectangles drawings on a transparent background:

image with lines, ellipses and rectangles drawings