How to Draw Graphics in C#

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#

  1. Configure the project to add Aspose.Imaging from the Nuget gallery
  2. Create an instance of JpegOptions class to customize the output JPEG image
  3. Instantiate the FileCreateSource object and set it as Source in the JpegOptions object
  4. Initialize an empty image by providing its size
  5. Create a Graphics class object for the empty image and clear its surface
  6. Create a Pen and Brush for drawing different shapes
  7. 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#

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#.

 English