14

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

2 回答 2

36

我用了你的代码,你快到了。您需要使 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");
        }
    }
}
于 2012-06-18T06:07:04.450 回答
-1
   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();
        }
于 2019-01-10T12:48:07.557 回答