Draw a Rectangle in C# using Mouse
Posted on September 7th, 2009 in C#, Tutorials | 15 Comments »

This tutorial will show you how to draw rectangle in C# using Mouse. You can normally draw a rectangle by putting the values manually. But that doesn’t always work. Sometimes you’ll require the rectangle to be drawn automatically (without putting values manually). This tutorial will show you how to do that. Let’s begin:
P.S: The source code for this tutorial is available for download. Scroll to the bottom of this post.
It is very simple and it isn’t as hard as it sounds.
First of all, declare the variable “rect”. Declaring it outside all the methods will let us access at anywhere in the current form.
Rectangle rect;
// Optional
public Form1()
{
InitializeComponent();
// Use the cross "+" cursor
this.Cursor = System.Windows.Forms.Cursors.Cross;
// This will reduce flicker (Recommended)
this.DoubleBuffered = true;
}
Now add MouseDown event to your form and use the following code:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
// "e.X" and "e.Y" are used to get MousePositionX and MousePositionY
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
The above code sets the X-coordinate and Y-coordinate of the upper-left corner of the rectangle.
Okay. Now add the MouseMove event to the form and use this code:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
// This makes sure that the left mouse button is pressed.
if (e.Button == MouseButtons.Left)
{
// Draws the rectangle as the mouse moves
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
this.Invalidate();
}
Almost done! Add the Paint event to your form and use this code:
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Replace "Color.Red" with any color and repalce "2" with any size you like.
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
That’s it! We are done.
The source code is available for download:
If you have any questions please don’t hesitate to post a comment.
15 Responses
[...] This post was Twitted by farooqaaa [...]
This above source code works fine.
I selected a text in the image by drawing a rectangle. How to capture(copy) and paste the selected text image…any idea…?
regards
M.Sathish Kumar
Hello Sathish,
I can’t understand you. Can you be more specific?
On which image did you draw the rectangle? And what are you trying to copy/paste?
Thanks.
Hi Farooz
I loaded a tiff image (which contains some text) in a picture box and using the above code i can draw a rectangle over a particular text in the TIFF image.
I want to convert that particular selected (via rectanlge) text image to bitmap image…can u pls help..?
regards
M.Sathish Kumar
Farooz
One more thing..
iam initlally converting the tiff image to bitmap and then only loading in to picture box… actually i want read the rectangular selection area to send as a bitmap parameter to the MODI object (for OCR conversion).
Thanks
M.Sathish Kumar
Hello Sathish,
Your problem is kinda complicated and I can’t help you with it.
You can ask your question here:
http://social.msdn.microsoft.com/Forums/en-US/newthread?forum=winforms
Write the question in detail, include the code you are using etc and you’ll sure get a good answer.
Hope that helps.
By the way, its “Farooq” not “Farooz”
Thanks.
Other variant is possible also
ok Farooq
Thanks
I work on this stuff full time. I couldn’t agree more. Great post! Good stuff! – Charly C&G
I want to quote your post in my blog. It can?
And you et an account on Twitter?
Yea, you can quote it as long as you add a link to the original post. And yes, I have got a Twitter account.
thanks,
Hey there I just wanted to say that I really enjoyed reading your blog but it’s taking a while to load up for me. I don’t have a slow internet connection either, so I think it may be your site? Or maybe there is just a lot of people trying to load up right now, either way I advise you look into it and also, keep up the nice blog posts.
Need to subscribe to this blog, great post. Found it on google.
hi!
Thank you so much ! You’re a life saver ! This is the only place where i could find a drawing example in which the mouse moves.