如何在 C# 中使用鼠标在 PictureBox.Image 上选择一个区域

本文关键字:选择 区域 一个 PictureBox 鼠标 Image | 更新日期: 2023-09-27 18:37:06

我只是想在我的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);
        }
    }

如何在 C# 中使用鼠标在 PictureBox.Image 上选择一个区域

我用了你的代码,你快到了。您需要使 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");
        }
    }
}
   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();
        }