Drawing a reversible rectangle

I got the code http://support.microsoft.com/kb/314945 to draw a rectangle reversing / rubber band. I added code to it, so when I leave the left mouse button, a rectangle is also created on the image, and then I use it to crop the image.

It works great. the only problem is that the rectangle of the rectangular shape does not start or end from where the mouse is ... there are very few differences, but still it's pretty noticeable. I use the same coordinates to draw a rectangle after that, which is drawn exactly where my mouse started and where it ended. help would be appreciated.

Here is the code: (The problem is fixed - adding code that others can extract from it) I made it a control, and I use it wherever I need it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CroppingControl
{
    public partial class CroppingImage : UserControl
    {
        Rectangle rc = new Rectangle();
        Boolean bHaveMouse;
        Point ptOriginal = new Point();
        Point ptLast = new Point();
        Image Pic;

        public CroppingImage()
        {
            InitializeComponent();
            pictureBox1.MouseDown += new MouseEventHandler(MyMouseDown);
            pictureBox1.MouseUp += new MouseEventHandler(MyMouseUp);
            pictureBox1.MouseMove += new MouseEventHandler(MyMouseMove);
            bHaveMouse = false;
        }


        public Image Image
        {
            set
            {
                pictureBox1.Image = value;
                Pic = value;
            }
            get
            {
                return pictureBox1.Image;
            }
        }

        public void MyMouseDown(Object sender, MouseEventArgs e)
        {
            pictureBox1.Image = Pic;
            // Make a note that we "have the mouse".

            bHaveMouse = true;
            // Store the "starting point" for this rubber-band rectangle.
            ptOriginal.X = e.X;
            ptOriginal.Y = e.Y;
            // Special value lets us know that no previous
            // rectangle needs to be erased.
            ptLast.X = -1;
            ptLast.Y = -1;
        }

        // Convert and normalize the points and draw the reversible frame.
        private void MyDrawReversibleRectangle(Point p1, Point p2)
        {
            Point px = p1;
            Point py = p2;

            // Convert the points to screen coordinates.
            p1 = PointToScreen(p1);
            p2 = PointToScreen(p2);
            // Normalize the rectangle.
            if (p1.X < p2.X)
            {
                rc.X = p1.X;
                rc.Width = p2.X - p1.X;
            }
            else
            {
                rc.X = p2.X;
                rc.Width = p1.X - p2.X;
            }
            if (p1.Y < p2.Y)
            {
                rc.Y = p1.Y;
                rc.Height = p2.Y - p1.Y;
            }
            else
            {
                rc.Y = p2.Y;
                rc.Height = p1.Y - p2.Y;
            }
            // Draw the reversible frame.
            ControlPaint.DrawReversibleFrame(rc, Color.Black, FrameStyle.Dashed);

            rc.X = px.X;
            rc.Y = px.Y;

        }
        // Called when the left mouse button is released.
        public void MyMouseUp(Object sender, MouseEventArgs e)
        {
            // Set internal flag to know we no longer "have the mouse".
            bHaveMouse = false;
            // If we have drawn previously, draw again in that spot
            // to remove the lines.
            if (ptLast.X != -1)
            {
                Point ptCurrent = new Point(e.X, e.Y);
                MyDrawReversibleRectangle(ptOriginal, ptLast);
                Graphics graphics = pictureBox1.CreateGraphics();
                Pen pen = new Pen(Color.Gray, 2);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                graphics.DrawRectangle(pen, rc);

            }
            // Set flags to know that there is no "previous" line to reverse.
            ptLast.X = -1;
            ptLast.Y = -1;
            ptOriginal.X = -1;
            ptOriginal.Y = -1;
        }
        // Called when the mouse is moved.
        public void MyMouseMove(Object sender, MouseEventArgs e)
        {
            Point ptCurrent = new Point(e.X, e.Y);
            // If we "have the mouse", then we draw our lines.
            if (bHaveMouse)
            {
                // If we have drawn previously, draw again in
                // that spot to remove the lines.
                if (ptLast.X != -1)
                {
                    MyDrawReversibleRectangle(ptOriginal, ptLast);
                }
                // Update last point.
                ptLast = ptCurrent;
                // Draw new lines.
                MyDrawReversibleRectangle(ptOriginal, ptCurrent);
            }
        }

    }
}
+3
source share
1 answer

ControlPaint.DrawReversibleFrameuses the coordinates of the screen to draw a rectangle on the screen (i.e. without taking into account the windows of your application), which is useful when acting on mouse actions of the mouse, since the mouse can move outside the application window. If you use these same coordinates for drawing in your application, they will be absent, since the coordinates on the control on which you draw refer to the initial state of the control (usually to its upper left corner).

, PointToClient() RectangleToClient() , ,

// panel`s OnPaint
Rectangle screenRectangle = ...
Rectangle clientRectangle = panel.RectangleToClient(screenRectangle);
graphics.DrawRectangle(Pens.Red, clientRectangle);
+2

Source: https://habr.com/ru/post/1789936/


All Articles