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.

C# Graphics Programming

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

// This code example demonstrates how to draw a closed curve, arc, and circle.
// Create a Bitmap
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialie Graphics from Bitmap
Graphics graphics = Graphics.FromImage(bitmap);
// Define a Pen to draw
Pen penBlue = new Pen(Color.Blue, 4);
// Draw a curve
graphics.DrawClosedCurve(penBlue, new Point[] { new Point(10, 700), new Point(250, 500), new Point(500, 10), new Point(750, 500), new Point(990, 700) });
// Draw an Arc
Pen penRed = new Pen(Color.Red, 2);
graphics.DrawArc(penRed, 0, 0, 700, 700, 0, 180);
// Draw an Ellipse
Pen penGreen = new Pen(Color.Green, 2);
graphics.DrawEllipse(penGreen, 10, 10, 500, 500);
// Save the bitmap as PNG
bitmap.Save("C:\\Files\\Drawing\\Curves.png");
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#.

// This code example demonstrates how to draw a Polygon and Rectangle.
// Create a Bitmap
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialie Graphics from Bitmap
Graphics graphics = Graphics.FromImage(bitmap);
// Define a Pen to draw
Pen penBlue = new Pen(Color.Blue, 4);
// Draw a Polygon
graphics.DrawPolygon(pen, new Point[] { new Point(100, 100), new Point(500, 700), new Point(900, 100) });
// Draw a Rectangle
Pen penRed = new Pen(Color.Red, 2);
graphics.DrawRectangle(penRed, 10, 10, 900, 700);
// Save the bitmap as PNG
bitmap.Save("C:\\Files\\Drawing\\Shapes.png");
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#.

// This code example demonstrates how to draw a Line.
// Create a Bitmap
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialie Graphics from Bitmap
Graphics graphics = Graphics.FromImage(bitmap);
// Define a Pen to draw
Pen penBlue = new Pen(Color.Blue, 4);
// Draw Lines
graphics.DrawLine(pen, 10, 700, 500, 10);
graphics.DrawLine(pen, 500, 10, 990, 700);
// Save the bitmap as PNG
bitmap.Save("C:\\Files\\Drawing\\Lines.png");
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#.

// This code example demonstrates how to draw a Path.
// Create a Bitmap
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialie Graphics from Bitmap
Graphics graphics = Graphics.FromImage(bitmap);
// Define a Pen to draw
Pen penBlue = new Pen(Color.Blue, 4);
// Initialize Graphics path
GraphicsPath path = new GraphicsPath();
// Add line 1
path.AddLine(100, 100, 1000, 400);
// Add line 2
path.AddLine(1000, 600, 300, 600);
// Add Rectangle
path.AddRectangle(new Rectangle(500, 350, 200, 400));
// Add Ellipse
path.AddEllipse(10, 250, 450, 300);
// Draw a Path
graphics.DrawPath(pen, path);
// Save the bitmap as PNG
bitmap.Save("C:\\Files\\Drawing\\Shapes.png");
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#.

// This code example demonstrates how to draw a Region.
// Create a Bitmap
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Initialie Graphics from Bitmap
Graphics graphics = Graphics.FromImage(bitmap);
// Add a Polygon
path.AddPolygon(new Point[] { new Point(100, 400), new Point(500, 100), new Point(900, 400), new Point(500, 700) });
// Initialize a Region
Region region = new Region(path);
// Inner Graphics Path
GraphicsPath innerPath = new GraphicsPath();
// Add a Rectangle
innerPath.AddRectangle(new Rectangle(300, 300, 400, 200));
// Exclude inner path
region.Exclude(innerPath);
// Define a solid brush
Brush brush = new SolidBrush(Color.Blue);
// Fill region
graphics.FillRegion(brush, region);
// Save the bitmap as PNG
bitmap.Save("C:\\Files\\Drawing\\Lines.png");
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