question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked $$ANON_USER$$ edited

Choosr points and draw

I want to pick points on the picture. These points converge into a recta.

Later i will use this rectange for picture cropping....

Firstly how can I draw this recta.?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
1 Vote"
DanielZhang-MSFT answered $$ANON_USER$$ commented

Hi PaulDares-7770,
You can use Graphics.DrawRectangle method to draw a rectangle in pictureBox paint event.
Here is my test code you can refer to.

 private Point clickLast = Point.Empty;
 private Point clickPrev = Point.Empty;
 private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
 {
     clickPrev = clickLast;
     clickLast = new Point(e.X,e.Y);
     if (clickPrev == Point.Empty) return;
     pictureBox1.Refresh();
       
 }
    
 private void pictureBox1_Paint(object sender, PaintEventArgs e)
 {
     int h = clickLast.Y - clickPrev.Y;
     int w = clickLast.X - clickPrev.X;           
     System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 3);
     e.Graphics.DrawRectangle(pen, clickPrev.X, clickPrev.Y, w, h);
     clickLast = Point.Empty;
 }

Test result:
82633-330.gif
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



330.gif (51.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks was just the answer I was looking for. It worked very well for me.

0 Votes 0 ·
cheong00 avatar image
0 Votes"
cheong00 answered cheong00 edited

Just use the grafikmat.DrawRectangle() to draw your rectangle.

Not sure why you need to set width of matbmp to (bmp.Width / 4) and set cutimg.X to (bmp.Width / 2). You may want to verify that.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Yes, the code needs to be changed. So how do I pick two points and draw a rectangle? With Mouseclick? I still can't do this....

0 Votes 0 ·

Your pictureBox1 receives mouse events to save that start point, and then calculate the width and height of rectangle to be drawn with the end point, Now just use the .DrawRectangle() function I mentioned to draw it on the shadow copy of image you created, and replace the drawn image to the picturebox (I see you've done this step on code)

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered $$ANON_USER$$ commented

If you want to draw a Selection Rectangle, you can find samples in some archived threads , like for example :
draw selection rectangle (VB, ~same code in C#)
Drag Selection Box
Adding Drag/Drop/Resizable Selection Rectangle to Image Editor
etc...

and the old MSDN KB :
How to draw a rubber band rectangle or a focus rectangle in Visual C#


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I will look at the sites you have posted. I hope I can do what I want. Thanks

0 Votes 0 ·