Choosr points and draw

Anonymous
2021-03-29T01:48:47.487+00:00

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.?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,223 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-03-30T07:10:50.067+00:00

    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.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points
    2021-03-29T02:14:32.17+00:00

    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. Castorix31 81,636 Reputation points
    2021-03-29T15:31:38.163+00:00

    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#