Working with Vector Graphics in C#

Aspose.Drawing for .NET makes it easy for you to draw vector graphics using C#. The API lets you work with a variety of different vector graphics such as arcs, Bezier spline, Cardinal Spline, closed curves, ellipses, lines and a  number of other types. This article contains C# examples for drawing different types of vector graphics using the API.

Draw Arc in C#

To drawn an arc using C#:

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawArc method of Graphics class object to draw an arc
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawArc(pen, 0, 0, 700, 700, 0, 180);
bitmap.Save("DrawArc.png");
Draw Arc

Draw Bezier Spline in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawBezier method of Graphics class object to draw Bezier Spline
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
PointF p1 = new PointF(0, 0); // start point
PointF c1 = new PointF(0, 800); // first control point
PointF c2 = new PointF(1000, 0); // second control point
PointF p2 = new PointF(1000, 800); // end point
graphics.DrawBezier(pen, p1, c1, c2, p2);
bitmap.Save("DrawBezierSpline.png");
Draw Bezier Spline

Draw Cardinal Spline in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawCurve method of Graphics class object to draw Cardinal Spline
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawCurve(pen, new Point[] { new Point(10, 700), new Point(250, 500), new Point(500, 10), new Point(750, 500), new Point(990, 700) });
bitmap.Save("DrawCardinalSpline.png");
Draw Cardinal Spline

Draw Closed Curve in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawClosedCurve method of Graphics class object to draw closed curve
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawClosedCurve(pen, new Point[] { new Point(100, 700), new Point(350, 600), new Point(500, 500), new Point(650, 600), new Point(900, 700) });
bitmap.Save("DrawClosedCurve.png");
Draw Closed Curve

Draw Ellipse in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawEllipse method of Graphics class object to draw Ellipse
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawEllipse(pen, 10, 10, 900, 700);
bitmap.Save("DrawEllipse.png");
Draw Ellipse

Draw Lines in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawLine method of Graphics class object to draw lines
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawLine(pen, 10, 700, 500, 10);
graphics.DrawLine(pen, 500, 10, 990, 700);
bitmap.Save("DrawLines.png");
Draw Lines

Draw Path in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Initialize a new object of GraphicsPath class and add Lines to its path collection
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
GraphicsPath path = new GraphicsPath();
path.AddLine(100, 100, 1000, 400);
path.AddLine(1000, 600, 300, 600);
path.AddRectangle(new Rectangle(500, 350, 200, 400));
path.AddEllipse(10, 250, 450, 300);
graphics.DrawPath(pen, path);
bitmap.Save("DrawPath.png");
Draw Path

Draw Polygon in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawPolygon method of Graphics class object to draw Polygon
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawPolygon(pen, new Point[] { new Point(100, 100), new Point(500, 700), new Point(900, 100) });
bitmap.Save("DrawPolygon.png");
Draw Polygon

Draw Rectangle in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Define a Pen object with desired parameters
  4. Use the DrawRectangle method of Graphics class object to draw Rectangle
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawRectangle(pen, 10, 10, 900, 700);
bitmap.Save("DrawRectangle.png");
Draw Rectangle

Fill Region in C#

  1. Instantiate an object of Bitmap class
  2. Initialize an object of Graphics class from this bitmap
  3. Instantiate a GraphicsPath class object
  4. Add polygon to the Path collection of GraphicsPath class object
  5. Use the FillRegion method of Graphics class to fill defined regions
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
GraphicsPath path = new GraphicsPath();
path.AddPolygon(new Point[] { new Point(100, 400), new Point(500, 100), new Point(900, 400), new Point(500, 700) });
Region region = new Region(path);
GraphicsPath innerPath = new GraphicsPath();
innerPath.AddRectangle(new Rectangle(300, 300, 400, 200));
region.Exclude(innerPath);
Brush brush = new SolidBrush(Color.Blue);
graphics.FillRegion(brush, region);
bitmap.Save("FillRegion.png");
Fill Region