C#ImageBox在MouseUp上清除矩形 [英] C# ImageBox Clear Rectangle on MouseUp

查看:238
本文介绍了C#ImageBox在MouseUp上清除矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a panel with multiple picturebox created at runtime.

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

The user will create a rectangle on any picture box and the selected part will be displayed on a preview picturebox.

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

I have successfully done the above using the below code.

问题

  1. 我想在mouseup事件时清除选择矩形.使用无效但不起作用. 来自如何清除图片框中的图形(矩形)
  2. 此外,当我滚动面板时,所有图片框上都会显示相同的矩形(鼠标选择).

  1. I want to clear the selection rectangle at mouseup event. Used invalidate but not working. From how to clear the graphics(rectangle shape) in picturebox
  2. Also, when I scroll the panel the same rectangle(mouse selection) is shown on all picturebox.

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

}

推荐答案

我会尝试这种方法:

首先,将Image变量设置为格式

First, make Image variable, in the scope of form

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

second, save current picture in that variable on MouseDown

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

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

lastly, on the end of MouseUp event, set Rectangle's width and height to zero and restore saved, original image

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

我不明白第二个问题.

这篇关于C#ImageBox在MouseUp上清除矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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