MouseUp上的C#ImageBox Clear Rectangle

时间:2017-05-17 09:44:11

标签: c# visual-studio mouseevent panel picturebox

我有一个在运行时创建了多个图片框的面板。

用户将在任何图片框上创建一个矩形,所选部分将显示在预览图片框上。

我已使用以下代码成功完成了上述操作。

问题

  1. 我想在mouseup事件中清除选择矩形。使用无效但无效。 来自how to clear the graphics(rectangle shape) in picturebox
  2. 此外,当我滚动面板时,所有图片框上都会显示相同的矩形(鼠标选择)。

        private void Picture_Paint(object sender, PaintEventArgs e)
    {
        if (Rect!=null && Rect.Width>0 && Rect.Height>0)
        {
            e.Graphics.FillRectangle(selectionBrush, Rect);
        }
    }
    
    private void Picture_MouseDown(object sender, MouseEventArgs e)
    {
        RecStartpoint = e.Location;
        ((PictureBox)sender).Invalidate();
    }
    
    private void Picture_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
    
        Point tempEndPoint = e.Location;
        Rect.Location = new Point(
            Math.Min(RecStartpoint.X, tempEndPoint.X),
            Math.Min(RecStartpoint.Y, tempEndPoint.Y));
        Rect.Size = new Size(
            Math.Abs(RecStartpoint.X - tempEndPoint.X),
            Math.Abs(RecStartpoint.Y - tempEndPoint.Y));
        ((PictureBox)sender).Invalidate();
    }
    
    private void Picture_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
    
        PictureBox org_pic = (PictureBox)(sender);
    
        Point RecEndpoint=e.Location;
    
    
        int xDown = Math.Min(RecStartpoint.X,RecEndpoint.X);
        int yDown = Math.Min(RecStartpoint.Y, RecEndpoint.Y);
        int xUp = Math.Max(RecStartpoint.X,RecEndpoint.X);
        int yUp = Math.Max(RecStartpoint.Y,RecEndpoint.Y);
    
        Rectangle rec = new Rectangle(xDown, yDown, Math.Abs(xUp - xDown), Math.Abs(yUp - yDown));
    
        xDown = xDown * org_pic.Image.Width / org_pic.Width;
        yDown = yDown * org_pic.Image.Height / org_pic.Height;
    
        xUp = xUp * org_pic.Image.Width / org_pic.Width;
        yUp = yUp * org_pic.Image.Height / org_pic.Height;
    
        rectCropArea = new Rectangle(xDown, yDown, Math.Abs(xUp - xDown), Math.Abs(yUp - yDown));
    
        pictureBox_preview_photo.Refresh();
        Bitmap sourceBitmap = new Bitmap(org_pic.ImageLocation);
        Graphics g = pictureBox_preview_photo.CreateGraphics();
        g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox_preview_photo.Width, pictureBox_preview_photo.Height), rectCropArea, GraphicsUnit.Pixel);
    
    }
    

1 个答案:

答案 0 :(得分:0)

我会尝试这种方法:

首先,在表单范围

中创建Image变量
public partial class Form1 : Form
{
    //variable for holding original image, before rectangle is drawn on it
    Image originalImage = null;

    public Form1()
    {
        InitializeComponent();
    }
    //rest of form's code...

第二,将当前图片保存在MouseDown

上的该变量中
private void Picture_MouseDown(object sender, MouseEventArgs e)
{
    //save it
    startImage = ((PictureBox)sender).Image;
    RecStartpoint = e.Location;
    ((PictureBox)sender).Invalidate();
}

最后,在MouseUp事件结束时,将Rectangle的宽度和高度设置为零并恢复已保存的原始图像

//snipped code
pictureBox_preview_photo.Refresh();
Bitmap sourceBitmap = new Bitmap(org_pic.ImageLocation);
Graphics g = pictureBox_preview_photo.CreateGraphics();
g.DrawImage(sourceBitmap, new Rectangle(0, 0, pictureBox_preview_photo.Width, pictureBox_preview_photo.Height), rectCropArea, GraphicsUnit.Pixel);

//make rectangle's widht and height 0 so that Paint event won't draw it
Rect.Width = Rect.Height = 0;
//restore image
this.Picture.Image = startImage;

我不明白第二个问题。