C#Windows窗体。 PictureBox上的可移动区域

时间:2011-02-09 19:22:45

标签: c# winforms picturebox

我正在使用像http://imgur.com/a2VKb

这样的图片

我设法在扫描图像的页面之间找到​​一条垂直线。但有时会出现一些错误,我需要为用户选择更改此行位置和角度。我认为这在PictureBox中会很好。

我需要以某种方式在带有当前图像的图片框上的两个可移动点之间画一条线。当我移动一个点时,线的位置和它的角度必须正确改变。

2 个答案:

答案 0 :(得分:2)

以下是您可以根据需要使用的示例代码。它主要使用4个事件:
- 油漆
- MouseDown
- MouseMove
- MouseUp

您可以将此代码复制粘贴到名为Form1的表单,并使用名为pictureBox1

的图片框

    int handleRadius = 3;
    int mPointMoveInProgress = 0;
    Point mPoint1, mPoint2;

    public Form1()
    {
        InitializeComponent();

        mPoint1 = new Point(50, 50); // Set correct default values
        mPoint1 = new Point(50, 300); // Set correct default values
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        // Draw line
        e.Graphics.DrawLine(new Pen(Color.Black, 2), mPoint1, mPoint2);

        Rectangle rectangle;

        // Draw first handle
        rectangle = new Rectangle(mPoint1.X - handleRadius, mPoint1.Y - handleRadius, handleRadius * 2, handleRadius * 2);
        e.Graphics.FillRectangle(Brushes.White, rectangle);
        e.Graphics.DrawRectangle(Pens.Black, rectangle);

        // Draw second handle
        rectangle = new Rectangle(mPoint2.X - handleRadius, mPoint2.Y - handleRadius, handleRadius * 2, handleRadius * 2);
        e.Graphics.FillRectangle(Brushes.White, rectangle);
        e.Graphics.DrawRectangle(Pens.Black, rectangle);
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        // Determine if a point is under the cursor. If so, declare that a move is in progress
        if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) mPointMoveInProgress = 1;
        else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) mPointMoveInProgress = 2;
        else mPointMoveInProgress = 0;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mPointMoveInProgress == 1) // If moving first point
        {
            mPoint1.X = e.X ;
            mPoint1.Y = e.Y ;
            Refresh();
        }
        else if (mPointMoveInProgress == 2) // If moving second point
        {
            mPoint2.X = e.X ;
            mPoint2.Y = e.Y ;
            Refresh();
        }
        else // If moving in the PictureBox: change cursor to hand if above a handle
        {
            if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) Cursor.Current = Cursors.Hand;
            else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) Cursor.Current = Cursors.Hand;
            else Cursor.Current = Cursors.Default;
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        // Declare that no move is in progress
        mPointMoveInProgress = 0;
    }

int handleRadius = 3; int mPointMoveInProgress = 0; Point mPoint1, mPoint2; public Form1() { InitializeComponent(); mPoint1 = new Point(50, 50); // Set correct default values mPoint1 = new Point(50, 300); // Set correct default values } private void pictureBox1_Paint(object sender, PaintEventArgs e) { // Draw line e.Graphics.DrawLine(new Pen(Color.Black, 2), mPoint1, mPoint2); Rectangle rectangle; // Draw first handle rectangle = new Rectangle(mPoint1.X - handleRadius, mPoint1.Y - handleRadius, handleRadius * 2, handleRadius * 2); e.Graphics.FillRectangle(Brushes.White, rectangle); e.Graphics.DrawRectangle(Pens.Black, rectangle); // Draw second handle rectangle = new Rectangle(mPoint2.X - handleRadius, mPoint2.Y - handleRadius, handleRadius * 2, handleRadius * 2); e.Graphics.FillRectangle(Brushes.White, rectangle); e.Graphics.DrawRectangle(Pens.Black, rectangle); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { // Determine if a point is under the cursor. If so, declare that a move is in progress if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) mPointMoveInProgress = 1; else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) mPointMoveInProgress = 2; else mPointMoveInProgress = 0; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (mPointMoveInProgress == 1) // If moving first point { mPoint1.X = e.X ; mPoint1.Y = e.Y ; Refresh(); } else if (mPointMoveInProgress == 2) // If moving second point { mPoint2.X = e.X ; mPoint2.Y = e.Y ; Refresh(); } else // If moving in the PictureBox: change cursor to hand if above a handle { if (Math.Abs(e.X - mPoint1.X) < handleRadius && Math.Abs(e.Y - mPoint1.Y) < handleRadius) Cursor.Current = Cursors.Hand; else if (Math.Abs(e.X - mPoint2.X) < handleRadius && Math.Abs(e.Y - mPoint2.Y) < handleRadius) Cursor.Current = Cursors.Hand; else Cursor.Current = Cursors.Default; } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { // Declare that no move is in progress mPointMoveInProgress = 0; }

答案 1 :(得分:1)

使用Graphics类。

虽然有关于绘图的新事件(在鼠标移动处理程序或需要它的地方):

// we don't need to change imageSource 
Image imgSourceCopy = imageSource.Clone as Image;

Graphics g = Graphics.FromImage(imgSourceCopy);

g.DrawLine(point1, point2);
pictureBox.Image = imgSourceCopy;

imgSourceCopy仅用于绘制线。 附:你好,来自利沃夫:)