如何在C#中用鼠标选择PictureBox.Image上的区域 [英] How to select an area on a PictureBox.Image with mouse in C#

查看:1236
本文介绍了如何在C#中用鼠标选择PictureBox.Image上的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在我的picturebox.image上放置一个选择,但是这比一些烦人的情况要糟糕得多。我想到了主图片框上的另一个图片框,但对我来说似乎太懒了。我需要知道是否有一种方法可以在picturebox上创建选择区域(该区域将是半透明的蓝色区域)。该图像将用鼠标绘制,并且不应更改正在处理的图像。

i just wanted to put a selection on my picturebox.image but this has just become worse than some little annoying situation. I thought on another picture box over the main picturebox but it seemed so lazy work to me. I need to know if there is a way to create a selection area (which is gonna be half transparent blue area) on a picturebox.image which im gonna draw with mouse and it shouldnt change the image im working on.

样本:

    // Start Rectangle
    //
    private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        // Determine the initial rectangle coordinates...
        RectStartPoint = e.Location;
        Invalidate();
    }

    // Draw Rectangle
    //
    private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;
        Point tempEndPoint = e.Location;
        Rect =
            new Rectangle(
                Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y),
                Math.Abs(RectStartPoint.X - tempEndPoint.X),
                Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
        Invalidate(Rect);
    }

    // Draw Area
    //
    private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        // Draw the rectangle...
        if (pictureBox1.Image != null)
        {
            Brush brush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));
            e.Graphics.FillRectangle(brush, Rect);
        }
    }


推荐答案

I使用您的代码,您就快到了。您需要使pictureBox1而不是矩形失效。我还为Rect添加了一个支票,这样它在未初始化或没有大小时就不会被绘制。

I used your code, you were nearly there. You needed to Invalidate the pictureBox1 instead of the rectangle. I also added a check for the Rect so it doesn't get drawn when it's not initialized or has no size.

另一个重要的变化:我仅创建一次Rectangle,我调整了它的位置和大小。

Another important change: I created the Rectangle only once and I adjusted its location and size. Less garbage to clean up!

EDIT

我添加了鼠标右键-

private Point RectStartPoint;
private Rectangle Rect = new Rectangle();
private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));

// Start Rectangle
//
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    // Determine the initial rectangle coordinates...
    RectStartPoint = e.Location;
    Invalidate();
}

// Draw Rectangle
//
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left)
        return;
    Point tempEndPoint = e.Location;
    Rect.Location = new Point(
        Math.Min(RectStartPoint.X, tempEndPoint.X),
        Math.Min(RectStartPoint.Y, tempEndPoint.Y));
    Rect.Size = new Size(
        Math.Abs(RectStartPoint.X - tempEndPoint.X),
        Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
    pictureBox1.Invalidate();
}

// Draw Area
//
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    // Draw the rectangle...
    if (pictureBox1.Image != null)
    {
        if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
        {
            e.Graphics.FillRectangle(selectionBrush, Rect);
        }
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        if (Rect.Contains(e.Location))
        {
            Debug.WriteLine("Right click");
        }
    }
}

这篇关于如何在C#中用鼠标选择PictureBox.Image上的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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