Graphics Programming in C#

Graphics programming in C# might seem easier or harder than you might think. In this article, you will learn how to create stunning vector graphics programmatically. Whether you’re creating graphics as a part of your current project or as the basis of your own game or app! You’ll learn to create beautiful visuals with ease. Let’s get started!

The following topics shall be covered in this article:

  1. C# Vector Graphics Programming API
  2. The Structure of a Vector Graphic
  3. Graphics Programming using C#
  4. Curves in Graphics Programming
  5. Polygons and Rectangles
  6. How to Draw Lines
  7. How to Draw Paths
  8. Draw a Fill Region

C# Vector Graphics Programming API - Free Download

To create stunning vector graphics in C#, we will be using the Aspose.Drawing for .NET API. It is a cross-platform 2D graphics library for drawing text, geometries, and images programmatically. It allows loading, saving, and manipulating the supported file formats.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Drawing

The Structure of a Vector Graphic

Vector graphics are made up of geometric shapes: lines, curves, and paths. These objects, with various properties such as length and color, can be manipulated by a C# program.

Graphics Programming using C#

We can paint or draw various types of shapes including curves, lines, rectangles, filled regions, ellipses, and polygons. The API provides various methods to draw these shapes. We can create a variety of different vector graphics programmatically by following the steps given below:

  1. Create an object of the Bitmap class.
  2. Initialize an object of the Graphics class from the bitmap.
  3. Define a Pen or SolidBrush class object with desired parameters.
  4. Use the shape-specific method(s) of the Graphics class to draw the desired shape(s).
  5. In the end, save the image.

Now, let’s see how to perform these steps in C#.

Curves in Graphics Programming using C#

We can draw arcs, circles, ellipses, or closed curves by using the following methods:

  • DrawArc() method to draw an arc.
  • DrawEllipse() method to draw an ellipse.
  • DrawClosedCurve() method to draw a closed curve.
  • DrawCurve() method to draw a curve.

Please follow the steps given below to draw a curved shape:

  1. Firstly, create an instance of the Bitmap class.
  2. Next, create the Graphics class object using the FromImage() method from the newly created Bitmap object.
  3. Then, define a Pen class object with the specified color and size.
  4. After that, call the DrawClosedCurve() method to draw a closed curve.
  5. Optionally, repeat the above steps to add an arc and ellipse.
  6. Finally, save the output image using the Save() method.

The following code sample shows how to paint curved shapes in C#.

Curves in Graphics Programming using C#

Curves in Graphics Programming using C#

Polygons and Rectangles in Graphics Programming using C#

We can draw polygons and rectangles by following the steps mentioned earlier. However, we need to use the following methods in step # 4:

  • DrawPolygon() method for drawing a polygon.
  • DrawRectangle() method to draw a rectangle.

The following code sample shows how to draw polygons and rectangles in C#.

Load-an-Image-in-Bitmap-using-CSharp

Load an Image in Bitmap using C#

Lines in Graphics Programming using C#

Similarly, we can draw lines by following the steps mentioned earlier. However, we need to use the DrawLine() method in step # 4 to draw a line.

The following code sample shows how to draw lines in C#.

Lines in Graphics Programming using C#

Lines in Graphics Programming using C#

Paths in Graphics Programming using C#

In vector graphics, a path represents a series of smooth straight lines defined by a start and end point, along with other points. We can draw a path by following the steps given below:

  1. Firstly, create an instance of the Bitmap class.
  2. Next, initialize the Graphics class object from the newly created Bitmap object using the FromImage() method.
  3. Then, define a Pen class object with the specified color and size.
  4. Next, create an instance of the GraphicsPath class.
  5. Then, add lines using the AddLine() method.
  6. After that, call the DrawPath() method with Pen and GraphicsPath objects.
  7. Finally, save the output image using the Save() method.

The following code sample shows how to draw a path in C#.

Paths in Graphics Programming using C#

Paths in Graphics Programming using C#

Fill Region in Graphics Programming using C#

We can draw a fill region by following the steps given below:

  1. Firstly, create an instance of the Bitmap class.
  2. Next, initialize the Graphics class object from the newly created Bitmap object using the FromImage() method.
  3. Then, create an instance of the GraphicsPath class.
  4. Meanwhile, add a polygon using the AddPolygon() method.
  5. Next, create an instance of the Region class.
  6. Then, add another GraphicsPath object and add a rectangle using the AddRectangle() method.
  7. Next, call the Exclude() method to exclude the inner path from the region.
  8. Then, define a SolidBrush class object with the specified color.
  9. After that, call the FillRegion() method with SolidBrush and GraphicsPath objects.
  10. Finally, save the output image using the Save() method.

The following code sample shows how to draw a filled region in C#.

Fill Region in Graphics Programming using C#

Fill Region in Graphics Programming using C#

Get Free Temporary License

You can get a free temporary license to try Aspose.Drawing for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a new bitmap;
  • draw rectangle, ellipse, etc. on a bitmap;
  • draw lines and paths;
  • save a Bitmap image as PNG in C#.

Besides graphics programming in C#, you can learn more about Aspose.Drawing for .NET using documentation and explore various features supported by the API. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also