Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用鼠标/绘制事件从图像中清除先前绘制的矩形?_C#_Winforms_Graphics - Fatal编程技术网

C# 如何使用鼠标/绘制事件从图像中清除先前绘制的矩形?

C# 如何使用鼠标/绘制事件从图像中清除先前绘制的矩形?,c#,winforms,graphics,C#,Winforms,Graphics,我正在设计一个应用程序,将图像加载到PictureBox中,并允许用户在上面绘制一个选择矩形。目前,我使用Paint事件和布尔值清除先前绘制的矩形(因为它是一个可拖动的选择框) 问题: 代码失败,因为没有从图像中清除上一个矩形。虽然绘制的每个矩形都是透明的,但效果是不透明的矩形,因为前面的矩形没有清除。如何清除这些矩形 逻辑: saveState默认为true。首次触发绘制事件时,将保存包含正常图像的状态。当触发MouseDown事件时,我们注册矩形的起始位置和一个表示正在绘制矩形的布尔值 当触

我正在设计一个应用程序,将图像加载到PictureBox中,并允许用户在上面绘制一个选择矩形。目前,我使用Paint事件和布尔值清除先前绘制的矩形(因为它是一个可拖动的选择框)

问题: 代码失败,因为没有从图像中清除上一个矩形。虽然绘制的每个矩形都是透明的,但效果是不透明的矩形,因为前面的矩形没有清除。如何清除这些矩形

逻辑:
saveState
默认为true。首次触发绘制事件时,将保存包含正常图像的状态。当触发MouseDown事件时,我们注册矩形的起始位置和一个表示正在绘制矩形的布尔值

当触发MouseMove事件时,我们在当前坐标处绘制一个矩形。由于绘制矩形时会触发绘制事件(我认为),并且
saveState
为false,因此我们在绘制矩形之前恢复正常图像

最后,当一个MouseUp事件被触发时,
saveState
被设置为true,因此最后绘制的矩形的图形状态被保存,我们回到了开始

我读过,但由于给我的印象是,它不是为在图像上绘制而设计的,而是直接在屏幕或表单上绘制的,我不确定这是我需要的

代码: 这是
CalculateRectangleDimensions
的代码(存储在静态库中):

public static int[]CalculateRectangleDimensions(intx1、inty1、intx2、inty2)
{
int[]维度=新的int[4];//x1,y1,宽度,高度

如果(x1Graphics.Save)在调用时不保存图形的全部内容,它只保存状态信息,如平移、缩放、变换等


如果要撤消已完成的绘图,则必须执行可逆绘制的操作,或者在撤消绘图时必须重新绘制原始图像。

Graphics.Save在调用时不保存图形的全部内容,它只保存状态信息,如平移、缩放、转换等表格等


如果要撤消已完成的绘图,必须执行可逆绘制的操作,或者在撤消绘图时必须重新绘制原始图像。

我应该使用吗?它允许我在图像本身上绘制吗?对于您正在执行的操作(鼠标拖动时临时绘制),我认为
DrawReversibleFrame
正是您想要的。您不需要在图像上绘制,您需要在用户拖动鼠标的屏幕上临时绘制,对吗?基本上,是的。一旦用户放开鼠标,我需要将透明矩形与图像一起保存,但对于选择过程,则不需要需要保存。我应该使用吗?它允许我在图像本身上绘制吗?用于您正在执行的操作(鼠标拖动时临时绘制),我认为
DrawReversibleFrame
正是您想要的。您不需要在图像上绘制,您需要在用户拖动鼠标的屏幕上临时绘制,对吗?基本上,是的。一旦用户放开鼠标,我需要将透明矩形与图像一起保存,但对于选择过程,则不需要需要保存。
public partial class MainWindow : Form
{
    private bool drawingRectangle;
    private int x1, y1, x2, y2;
    private Image currentImage;
    private GraphicsState previousState;
    private bool saveState;

    public MainWindow()
    {
        InitializeComponent();
        this.drawingRectangle = false;
        this.saveState = true;
    }

    private void EditorPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        this.x1 = e.X;
        this.y1 = e.Y;
        this.drawingRectangle = true;
    }

    private void EditorPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (this.drawingRectangle)
        {
            this.x2 = e.X;
            this.y2 = e.Y;
            Graphics g = Graphics.FromImage(this.currentImage);
            int[] dim = ImageLibrary.CalculateRectangleDimensions(this.x1, this.y1, this.x2, this.y2);
            g.FillRectangle(new SolidBrush(Color.FromArgb(100, 128, 255, 255)), dim[0], dim[1], dim[2], dim[3]);
            this.Refresh();
        }
    }

    private void EditorPictureBox_Paint(object sender, PaintEventArgs e)
    {
        if (this.saveState)
        {
            this.previousState = e.Graphics.Save();
            this.saveState = false;
        }
        else
            e.Graphics.Restore(this.previousState);
    }

    private void EditorPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (this.drawingRectangle)
        {
            this.drawingRectangle = false;
            // When the mouse click is released, save the graphics state
            this.saveState = true; 
        }
    }

    private void LoadImage2Button_Click(object sender, EventArgs e)
    {
        this.currentImage = Image.FromFile("goat2.jpg");
        this.EditorPictureBox.Image = this.currentImage;
    }   
}
public static int[] CalculateRectangleDimensions(int x1, int y1, int x2, int y2)
{
    int[] dimensions = new int[4]; // x1, y1, width, height
    if (x1 <= x2) // Mouse was dragged to the right
    {
        dimensions[0] = x1;
        dimensions[2] = x2 - x1;
    }
    else // Mouse was dragged to the right
    {
        dimensions[0] = x2;
        dimensions[2] = x1 - x2;
    }

    if (y1 <= y2) // Mouse was dragged up
    {
        dimensions[1] = y1;
        dimensions[3] = y2 - y1;
    }
    else // Mouse was dragged down
    {
        dimensions[1] = y2;
        dimensions[3] = y1 - y2;
    }
    return dimensions;
}