C# 如何在PictureBox上选择一个区域。C语言中的鼠标图像

C# 如何在PictureBox上选择一个区域。C语言中的鼠标图像,c#,.net,winforms,system.drawing,C#,.net,Winforms,System.drawing,我只是想在我的picturebox.image上添加一个选项,但这比一些令人讨厌的小情况更糟糕。我在主图片盒上方的另一个图片盒上思考,但它对我来说似乎太懒了。我需要知道是否有办法在picturebox.image上创建一个选择区域,该区域将是半透明的蓝色区域,我将用鼠标绘制该区域,并且它不应更改我正在处理的图像 样本: // Start Rectangle // private void pictureBox1_MouseDown(object sender, Syste

我只是想在我的picturebox.image上添加一个选项,但这比一些令人讨厌的小情况更糟糕。我在主图片盒上方的另一个图片盒上思考,但它对我来说似乎太懒了。我需要知道是否有办法在picturebox.image上创建一个选择区域,该区域将是半透明的蓝色区域,我将用鼠标绘制该区域,并且它不应更改我正在处理的图像

样本:

    // 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);
        }
    }

我用了你的密码,你就快到了。您需要使pictureBox1而不是矩形无效。我还为Rect添加了一个检查,以便在它未初始化或没有大小时不会绘制它

另一个重要的变化是:我只创建了一次矩形,并调整了它的位置和大小。减少垃圾清理

编辑

我为矩形添加了鼠标右键单击处理程序

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");
        }
    }
}

另外,您能告诉我如何在该选择上创建右键单击事件吗?顺便说一句:在您的实现中,笔刷会一次又一次地创建。尽量避免这种情况。我也会为此调整我的代码。为什么一次又一次地创建笔刷是个问题?这是一个昂贵的手术吗?@kdbanman-是的。除了它是多余的之外,它还要求使用非托管资源。当您不处理这些画笔和其他GDI对象时,仍将分配此内存。根据鼠标移动的频率,代码可能会因此泄漏相当多的内存。另请参见此答案:@kdbanman-Invalidate导致重新绘制PictureBox。重画调用引发绘制事件,绘制事件处理程序绘制矩形。这个解决方案的好处是,每当PictureBox被重新绘制、移动、调整大小、缩放时。。。矩形将紧跟其后,并且始终处于同步状态。此外,Paint eventhandler接收对绘制矩形所需的图形对象的引用,因此我们不必自己创建和处理graphics对象。因此,是否要在pictureBox中的图像上创建选择框?选择框的作用是否与在桌面上单击并拖动以创建透明的蓝色方框相同?请添加一些说明,说明您的代码的作用以及它如何提供问题的答案。我们感谢您花时间提供解决方案,一个好的答案还应该有一些描述,说明为什么它会解决OP的问题,如果可能的话,指出OP需要解决的确切问题。滚动较大的代码片段通常会让人感到厌烦。尝试编辑以使其对OP和社区都更有用。我必须更改=>这一行:Bitmap sourceBitmap=new Bitmap SrcPicBox.Image,SrcPicBox.Width,SrcPicBox.Height;至:Bitmap sourceBitmap=新位图SrcPicBox.Image、SrcPicBox.Image.Width、SrcPicBox.Image.Height;为了得到正确的结果。
   private int xUp, yUp, xDown,yDown;
        private Rectangle rectCropArea;
   private void SrcPicBox_MouseUp(object sender, MouseEventArgs e)
        {
            //pictureBox1.Image.Clone();
            xUp = e.X;
            yUp = e.Y;
            Rectangle rec = new Rectangle(xDown,yDown,Math.Abs(xUp xDown),Math.Abs(yUp-yDown));
            using (Pen pen = new Pen(Color.YellowGreen, 3))
            {

                SrcPicBox.CreateGraphics().DrawRectangle(pen, rec);
            }
            rectCropArea = rec;
        }
 private void SrcPicBox_MouseDown(object sender, MouseEventArgs e)
        {
            SrcPicBox.Invalidate();

            xDown = e.X;
            yDown = e.Y;
        }
 private void btn_upload_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
          //  PictureBox SrcPicBox = new PictureBox();
            opf.Filter = "ALL images(*.*)|*.*";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                string name = opf.SafeFileName;
                string filepath = opf.FileName;
                File.Copy(filepath, name, true);
                SrcPicBox.Image = Image.FromFile(opf.FileName);
            }
 private void btn_crop_Click(object sender, EventArgs e)
        {
            pictureBox3.Refresh();
            //Prepare a new Bitmap on which the cropped image will be drawn
            Bitmap sourceBitmap = new Bitmap(SrcPicBox.Image, SrcPicBox.Width, SrcPicBox.Height);
            Graphics g = pictureBox3.CreateGraphics();

            //Draw the image on the Graphics object with the new dimesions
            g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox3.Width, pictureBox3.Height), rectCropArea, GraphicsUnit.Pixel);
            sourceBitmap.Dispose();
        }