This quick tutorial describes how to draw graphics in C#. It provides complete steps and a runnable code to explain the process in quite an easier way. While using C# drawing shapes of different types are demonstrated in a JPEG file however you can create a variety of other types of images as well.
Steps to Draw Graphics in C#
- Configure the project to add Aspose.Imaging from the Nuget gallery
- Create an instance of JpegOptions class to customize the output JPEG image
- Instantiate the FileCreateSource object and set it as Source in the JpegOptions object
- Initialize an empty image by providing its size
- Create a Graphics class object for the empty image and clear its surface
- Create a Pen and Brush for drawing different shapes
- Draw closed curves, rectangles and save the image as a JPEG file
By using C# drawing of different shapes and lines are described in these steps. After adding the required libraries, we need to create a blank image that is linked with the Graphics class object. This graphics object contains a lot of functions and properties to draw different types of shapes and lines which can be colored or filled using different pens and brushes as demonstrated in the sample code.
Code to Draw Graphics in C#
using Aspose.Imaging; | |
using Aspose.Imaging.Brushes; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
namespace DrawGraphicsInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to draw graphics in CSharp | |
{ | |
// Create and instantiate a license to draw graphics in CSharp | |
Aspose.Imaging.License licDrawGraphics= new Aspose.Imaging.License(); | |
licDrawGraphics.SetLicense("Aspose.Imaging.lic"); | |
// Instantiate the JpegOptions and set the color type | |
JpegOptions jpegOptions = new JpegOptions(); | |
jpegOptions.ColorType = Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode.Rgb; | |
// Set value of JpegOptions.Source to a newly created FileCreateSource class object | |
jpegOptions.Source = new FileCreateSource("SampleImage_out.jpeg", false); | |
using (var jpegImage = Image.Create(jpegOptions, 400, 400)) | |
{ | |
var jpegGraphics = new Graphics(jpegImage); | |
// Clear the image and set its background color | |
jpegGraphics.Clear(Color.LightSkyBlue); | |
// Create a Pen for drawing and set its color | |
var pen = new Pen(Color.DarkCyan); | |
// Draw closed curve by providing list of points | |
jpegGraphics.DrawClosedCurve(pen, new PointF[]{new PointF(50,50),new PointF(150,250),new PointF(350,115),new PointF(75,123),new PointF(23,5)}); | |
// Create a brush to fill the drawing | |
SolidBrush brush = new SolidBrush(Color.Chocolate); | |
// Create filled rectangles using the specified brush | |
jpegGraphics.FillRectangles(brush, new Rectangle[]{new Rectangle(100,100,30,40),new Rectangle(200,200,30,25)}); | |
// Save the resultant image | |
jpegImage.Save(); | |
} | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
This code in C# draw graphics by creating an image and then drawing different types of shapes on it. Here we have used JpegOptions however you may use any of the options like BmpOptions, GifOptions, PngOptions, SvgOptions, and TiffOptions to name a few. Similarly, you can draw an arc, bezier, curve, closed curve, eclipse, lines, polygon, rectangle, etc.
This tutorial has guided us to draw different types of shapes on images. If you want to learn to resize these images, refer to the article on how to resize image in C#.