Draw Rectangle C#

When working with images and graphics in C#, you may find the need to draw rectangles on them for various purposes such as highlighting areas of interest, creating bounding boxes, or simply adding visual elements to your images. In this blog post, we will explore how to draw rectangles on images and bitmaps in C#.

Draw Rectangle in C#

To draw a rectangle in C#, you can follow these steps:

  • Install Conholdate.Total for .NET in your environment.
  • Initialize a Bitmap class object.
  • Create a Graphics class instance and a brush while specifying its color.
  • Create a Pen and draw a rectangle.
  • Export the output drawing with the Save method.

The sample code below demonstrates how to draw a rectangle in C#:

The screenshot below shows the output rectangle generated using the above code snippet:

Create Rectangle C#

Draw Filled Rectangle on an Image in C#

Sometimes you may need to create custom rectangles with different lengths, widths, border colors, fill colors, etc. If you want to draw a filled (solid) rectangle instead of just an outline, please follow the steps below:

  • Configure Conholdate.Total for .NET in your system.
  • Create a Bitmap class instance.
  • Initialize a Graphics class instance and create a brush while selecting the fill color.
  • Draw a rectangle shape with the FillRectangle method.
  • Save the generated drawing using the Save method.

The code snippet below shows how to draw a filled rectangle in C#:

The following image shows a filled rectangle generated using this sample code:

Draw Rectangle on Image Bitmap C#

Free Evaluation License

You can get a free temporary license to test different features of the API to their full capacity.

Summing Up

Drawing rectangles on images in C# is a common task when working with image processing, computer vision, or any application that requires visual annotations. You can easily draw both outlined and filled rectangles on images and bitmaps in C#. This flexibility allows you to create informative and visually appealing images for your applications. In case of any queries, please feel free to get in touch with us via the forum.


How can I change the outline color or thickness of the rectangle when drawing it in C#?

You can customize the outline color and thickness of the rectangle by using the Pen class and KnownColor enumeration.

Can I draw multiple rectangles on a single image in C#?

Yes, you can draw multiple rectangles on a single image.

What are some common use cases for drawing rectangles on images in C#?

Drawing rectangles on images in C# is useful for various applications, including object detection and tracking in computer vision, image annotation for educational or presentation purposes, and creating user interface elements in graphical applications.

How do I specify the size and position of a rectangle in C#?

When drawing a rectangle in C#, you need to specify the coordinates (X and Y) of the rectangle’s top-left corner and its width and height.

See Also