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#:
// Initialize a Bitmap class object | |
Bitmap bitmap = new Bitmap(1000, 1000, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
// Create graphics class instance | |
Graphics graphics = Graphics.FromImage(bitmap); | |
// Create a brush while specifying its color | |
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.Blue)); | |
// Create a pen | |
Pen pen = new Pen(brush); | |
// Draw rectangle | |
graphics.DrawRectangle(pen, 100, 200, 800, 600); | |
// Save output drawing | |
bitmap.Save("Rectangle.png"); |
The screenshot below shows the output rectangle generated using the above code snippet:
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#:
// Initialize a Bitmap class object | |
Bitmap bitmap = new Bitmap(1000, 1000, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
// Create graphics class instance | |
Graphics graphics = Graphics.FromImage(bitmap); | |
// Create a brush while specifying its color | |
Brush brush = new SolidBrush(Color.FromKnownColor(KnownColor.MediumVioletRed)); | |
// Draw rectangle | |
graphics.FillRectangle(brush, 100, 100, 800, 600); | |
// Save output drawing | |
bitmap.Save("Fill-Rectangle.png"); |
The following image shows a filled rectangle generated using this sample code:
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.
FAQs
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.