绘制矩形可逆 [英] Drawing reversible rectangle

查看:225
本文介绍了绘制矩形可逆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 http://support.microsoft.com/kb/314945 代码画一个可逆/橡皮筋矩形。我添加代码,这样,当我离开鼠标左键也是在图像上创建一个矩形,然后我利用它来进行裁剪图像。



本作品精湛。唯一的问题是,橡皮矩形亘古不变的开始或结束在那里的老鼠......有很少的性差异,但仍是相当显着。我使用相同的共ords事后绘制矩形被精确地绘制我的鼠标开始的地方,并在那里结束。 。帮助将不胜感激。



下面是代码:(GOT发出定额 - 添加代码,其他人可以从中受益)
我做了它的控制和!我用它无论我需要它

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Drawing中;
使用System.Data这;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;

命名空间CroppingControl
{
公共部分类CroppingImage:用户控件
{
矩形RC =新的Rectangle();
布尔bHaveMouse;
点ptOriginal =新点();
点ptLast =新点();
图像Pic;

公共CroppingImage()
{
的InitializeComponent();
pictureBox1.MouseDown + =新MouseEventHandler(MyMouseDown);
pictureBox1.MouseUp + =新MouseEventHandler(MyMouseUp);
pictureBox1.MouseMove + =新MouseEventHandler(MyMouseMove);
bHaveMouse = FALSE;
}


公共图片图片
{

{
pictureBox1.Image =价值;
PIC =价值;
}
得到
{
返回pictureBox1.Image;
}
}

公共无效MyMouseDown(对象发件人,MouseEventArgs E)
{
pictureBox1.Image =产品图;
//记下我们鼠标。

bHaveMouse = TRUE;
//将这个橡皮筋矩形的起点。
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
//特殊的价值让我们知道,以前没有
//矩形需要被删除。
ptLast.X = -1;
ptLast.Y = -1;
}

//转换和规范点,绘制可逆的框架。
私人无效MyDrawReversibleRectangle(点P1,P2点)
{
点PX = P1;
点PY = P2;

//将指向屏幕坐标。
P1 = PointToScreen(P1);
P2 = PointToScreen(P2);
//规范化矩形。
如果(p1.X< p2.X)
{
rc.X = p1.X;
rc.Width = p2.X - p1.X;
}
,否则
{
rc.X = p2.X;
rc.Width = p1.X - p2.X;
}
如果(p1.Y< p2.Y)
{
rc.Y = p1.Y;
rc.Height = p2.Y - p1.Y;
}
,否则
{
rc.Y = p2.Y;
rc.Height = p1.Y - p2.Y;
}
//绘制可逆的框架。
ControlPaint.DrawReversibleFrame(RC,Color.Black,FrameStyle.Dashed);

rc.X =逻辑Px.x;
rc.Y = PX.Y;

}
//鼠标左键被释放时调用。
公共无效MyMouseUp(对象发件人,MouseEventArgs E)
{
//设置内部标志,知道我们不再是有老鼠。
bHaveMouse = FALSE;
//如果我们以前绘制的,在这点
//再次提请去除线。
如果(ptLast.X!= -1)
{
点ptCurrent =新的点(e.X,e.Y);
MyDrawReversibleRectangle(ptOriginal,ptLast);
显卡显卡= pictureBox1.CreateGraphics();
喷喷=新朋(Color.Gray,2);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
graphics.DrawRectangle(笔,RC);

}
//设置标志就知道有没有以前的行反转。
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
}
//当鼠标移动时调用。
公共无效MyMouseMove(对象发件人,MouseEventArgs E)
{
点ptCurrent =新的点(e.X,e.Y);
//如果我们鼠标,那么,我们把我们的线。
如果(bHaveMouse)
{
//如果我们先前得出,
再次提请//那个斑点去除线。
如果(ptLast.X!= -1)
{
MyDrawReversibleRectangle(ptOriginal,ptLast);
}
//更新最后一点。
ptLast = ptCurrent;
//绘制新线路。
MyDrawReversibleRectangle(ptOriginal,ptCurrent);
}
}

}
}


解决方案

ControlPaint.DrawReversibleFrame 使用屏幕坐标,以在屏幕上绘制一个矩形(即不尊重你的应用程序的窗口)这在拖动鼠标操作充当鼠标可能之外将应用程序窗口的时候非常有用。如果您在使用这些同样的坐标生在应用程序中作画,那么他们会出作为控制的坐标后,你所画的是相对于控件的原点(通常是其左上角)



要使用的屏幕坐标,首先需要使用到它们转换成控制坐标的 PointToClient() RectangleToClient()一经你是画中的控制,如:

 <方法code> // panel`s的OnPaint 
矩形screenRectangle = ...
矩形clientRectangle = panel.RectangleToClient(screenRectangle);
graphics.DrawRectangle(Pens.Red,clientRectangle);


I got code from http://support.microsoft.com/kb/314945 to draw a reversible/rubber band rectangle. I added code to it so that when i leave the left mouse button a rectangle is also created on the image and then i use that for cropping the image.

This works superb. the only problem is that the rubberband rectangle doesnot start or end from where the mouse is... there is very little diference but still it is quite notable. i use the same co-ords to draw the rectangle afterwards which is drawn exactly where my mouse started and where is ended. help would be appreciated.

Here is the code: (Got issue fixed - adding code that others can benefit from it) I have 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);
            }
        }

    }
}

解决方案

ControlPaint.DrawReversibleFrame uses screen co-ordinates to draw a rectangle on the screen (i.e. without respect to your application's windows) which is useful when acting upon drag mouse actions as the mouse may move outside of the application window. If you are using these very same co-ordinates raw to paint in your application, then they will be out as the co-ordinates on the control upon which you are painting are with respect to the control's origin (usually its top-left corner).

To use the screen co-ordinates, you first need to convert them into control co-ordinates using the PointToClient() or RectangleToClient() methods on the control upon which you are painting, e.g.

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

这篇关于绘制矩形可逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆