Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to draw multiple rectangle in Picturebox using c#?

I want to draw multiple rectangles on Mousemove event.

I have tried the below code to draw single rectangle in picturebox but not multple rectangles



bool m_Drawing = false;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (m_Drawing)
{
selection = RectangleFromPoints(m_Start, e.Location);
//Update selection
pictureBox1.Invalidate();
}
if (mouseDown)
{
ep = e.Location;
pictureBox1.Invalidate();
}
}

private Rectangle RectangleFromPoints(Point p1, Point p2)
{
int x = 0;
int y = 0;

if (p1.X <= p2.X)
{
x = p1.X;
}
else
{
x = p2.X;
}

if (p1.Y <= p2.Y)
{
y = p1.Y;
}
else
{
y = p2.Y;
}

return new Rectangle(x, y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{

if (selection != null)
{
//There is a selection rectangle so draw it
e.Graphics.FillRectangle(hbr, selection);
}
}
Please help me on this.
Posted
Updated 26-Feb-14 17:08pm
v6
Comments
Sergey Alexandrovich Kryukov 27-Feb-14 0:24am    
Why, why, why? Why not drawing on something designed to draw on?
—SA
SukirtiShetty 27-Feb-14 4:13am    
Here I need to redact some portion of image by drawing multiple rectangles. I tried above code, but its not working.

Here I am loading image in a picturebox and drawing multiple rectangles in the image and save that image in the virtual folder.

Another pain point is that, when I set picturebox size mode to zoom and if I draw rectangle it will not be in the selected position. It will be in the top right corner position.
Sergey Alexandrovich Kryukov 27-Feb-14 11:25am    
I say, you should forget about PictureBox, to start with. Where do you need to draw something: screen, printer, bitmap, all of the above? what's the scenario? In all cases, PictureBox won't help at all, will only distract you from doing it...
—SA
SukirtiShetty 28-Feb-14 0:32am    
Will you Please give me idea how can I achieve this without using Picturebox.

thanks in advance
SukirtiShetty 1-Mar-14 6:59am    
I used Canvas to load image. It worked well. Now i want to save that image with drawing so please give me idea how to save image.



Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900