Draw Lines in C#

If you are looking for a way to draw lines in C#, you’ve come to the right place. Drawing shapes is a common task in C# graphic programming, and lines are among the most fundamental shapes. In this blog post, you will learn how to programmatically draw lines in C#.

This article covers the following topics:

  1. C# Graphics API to Draw Lines
  2. Draw a Line in C#
  3. Draw Lines in C#
  4. Free Learning Resources

C# Graphics API to Draw Lines

We will use Aspose.Drawing for .NET to draw lines in C#. With the help of Aspose.Drawing for .NET, developers can generate vector graphics in various formats, edit them and convert them as well. It offers a wide range of image manipulation operations, including cropping, resizing, rotating, flipping, and watermarking. With a variety of image manipulation options, it becomes an excellent selection for developers requiring image creation and editing capabilities within their applications.

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

PM> Install-Package Aspose.Drawing

Draw a Line in C#

In order to draw a line, the API provides the following methods. Each of the following methods draws a line connecting the two points specified by the coordinate pairs.

DrawLine(Pen, Point, Point) // Draws a line connecting two Point structures.

DrawLine(Pen, PointF, PointF) // Draws a line connecting two PointF structures.

DrawLine(Pen, float, float, float, float) // Draws a line connecting the two points specified by the coordinate pairs.

DrawLine(Pen, int, int, int, int) // Draws a line connecting the two points specified by the coordinate pairs.

We can easily draw a line by following the steps below:

  1. Create an instance of the Bitmap class.
  2. Create a new Graphics object from the Bitmap object using the FromImage() method.
  3. Initialize a Pen class object with the specified color and size.
  4. After that, call the DrawLine() method to draw a line with a specified coordinate points.
  5. Finally, save the output image using the Save() method.

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

Draw Lines in C#

Draw Lines in C#

Draw Lines in C#

We can also draw a series of line segments that connect an array of point structures using the following methods:

DrawLines(Pen, PointF[]) // Draws a series of line segments that connect an array of PointF structures.

DrawLines(Pen, Point[]) // Draws a series of line segments that connect an array of Point structures.

We can draw a series of lines by following the steps below:

  1. Create an instance of the Bitmap class.
  2. Create a new Graphics object from the Bitmap object using the FromImage() method.
  3. Initialize a Pen class object with the specified color and size.
  4. After that, call the DrawLines() method on specified array of point structures.
  5. Finally, save the output image using the Save() method.

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

Draw a series of line segments in C#

Draw a series of line segments in C#

Get a Free License

You can get a free temporary license to evaluate the API in its full capacity.

C# Draw Lines – Free Learning Resources

Besides drawing lines, you can learn more about drawing basic shapes and explore various other features of the library using the resources below:

Conclusion

In this article, we have learned how to draw lines in C#. We have explored various methods to draw a line or a series of lines programmatically using Aspose.Drawing for .NET API. Using Aspose.Drawing, you have the ability to utilize vector graphics, including lines, curves, and shapes, and also insert text in various font types, sizes, and styles onto raster images. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also