How to draw an image

How to draw an image

         With Aspose.Imaging you can use several methods to draw graphics primitives such as DrawLine, DrawEllipse, DrawRectangle, DrawArc, DrawBezier and DrawString. You need to create a graphic surface to draw an image from scratch and set up graphic properties. We will create a surface of 100 x 100 pixels with 32 bits per pixel in our example and set the background to yellow color with Clear method. Then we can define a drawing tool, for example, a Pen with a Blue color, to draw a dotted line or use SolidBrush for continuous lines with different colors, followed by X, Y coordinates of the start and end points of the lines:

using Aspose.Imaging;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System.IO;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
// Creates an instance of FileStream
using (FileStream stream = new FileStream(dataDir + "result.bmp", FileMode.Create))
{
// Create an instance of BmpOptions and set its various properties
BmpOptions saveOptions = new BmpOptions();
saveOptions.BitsPerPixel = 32;
saveOptions.Source = new StreamSource(stream);
// Create an instance of Image
using (Image image = Image.Create(saveOptions, 100, 100))
{
// Create and initialize an instance of Graphics class and Clear Graphics surface
Graphics graphic = new Graphics(image);
graphic.Clear(Color.Yellow);
// Draw two dotted diagonal lines by specifying the Pen object having blue color and co-ordinate Points
graphic.DrawLine(new Pen(Color.Blue), 9, 9, 90, 90);
graphic.DrawLine(new Pen(Color.Blue), 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.Red)), new Point(9, 9), new Point(9, 90));
graphic.DrawLine(new Pen(new SolidBrush(Color.Aqua)), new Point(9, 90), new Point(90, 90));
graphic.DrawLine(new Pen(new SolidBrush(Color.Black)), new Point(90, 90), new Point(90, 9));
graphic.DrawLine(new Pen(new SolidBrush(Color.White)), new Point(90, 9), new Point(9, 9));
image.Save();
}
}
File.Delete(dataDir + "result.bmp");

         A more detailed description of drawing lines, ellipses, rectangles, arcs, Bezier curves and text strings, you can find in the Aspose drawing images documentation.

Example image with lines, ellipses and rectangles drawings on transparent background:

image with lines, ellipses and rectangles drawings