C# 无法使用picturebox上的退格键删除我在C中绘制的线#

C# 无法使用picturebox上的退格键删除我在C中绘制的线#,c#,C#,这就是我到目前为止所做的。我一直在搜索代码,并尝试将其应用到我的程序中。然而,它根本不起作用 public Form1() { InitializeComponent(); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); } int originalX, originalY; int startX, startY, e

这就是我到目前为止所做的。我一直在搜索代码,并尝试将其应用到我的程序中。然而,它根本不起作用

    public Form1()
    {
        InitializeComponent();
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
    }
    int originalX, originalY;
    int startX, startY, endX, endY;
    int pointX1, pointY1;
    int pointX2, pointY2;
    Graphics g;
    int i = 0, j = 0;
    Bitmap image;


    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            if (i == 0)
            {
                image = (Bitmap)pictureBox1.Image;
                pictureBox1.Invalidate();
                originalX = startX = e.X;
                originalY = startY = e.Y;
                 g = Graphics.FromImage(image);
            }

        }

    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            pictureBox1.Refresh();
            g = pictureBox1.CreateGraphics();
            if (j < 10000000)
            {
                if (i != 0  )
                {
                    startX = pointX1;
                    startY = pointY1;
                }

                pointX2 = e.X;
                pointY2 = e.Y;
                g.DrawLine(Pens.Black, new Point(startX, startY), new Point(pointX2, pointY2));
                j++;
            }

        }

    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            g = Graphics.FromImage(image);
            if (i <100000000 )
            {
                if (i != 0)
                {
                    startX = pointX1;
                    startY = pointY1;
                }
                pointX1 = e.X;
                pointY1 = e.Y;
                g.DrawLine(Pens.Black, new Point(startX, startY), new Point(pointX1, pointY1));
            }

          }
            i++;
        }

    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = Image.FromFile(@"C:\Users\student03\Documents\image\Sky2.jpg");
    }

    private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            g = Graphics.FromImage(image);
            g.DrawLine(Pens.Black, new Point(pointX1, pointY1), new Point(originalX, originalY));
            pictureBox1.Image = image;
            i = 0;
        }
    }
   private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Back)
        { 
                pictureBox1.Invalidate();
        }
public Form1()
{
初始化组件();
this.KeyDown+=new System.Windows.Forms.KeyEventHandler(this.Form1\u KeyDown);
}
int originalX,originalY;
int startX、startY、endX、endY;
int点x1,点y1;
int点x2,点y2;
图形g;
int i=0,j=0;
位图图像;
私有void pictureBox1\u MouseDown(对象发送方,MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
如果(i==0)
{
image=(位图)pictureBox1.image;
pictureBox1.Invalidate();
原始X=startX=e.X;
原始=startY=e.Y;
g=图形。FromImage(图像);
}
}
}
私有void pictureBox1\u MouseMove(对象发送方,MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
g=pictureBox1.CreateGraphics();
如果(j<10000000)
{
如果(i!=0)
{
startX=pointX1;
startY=点1;
}
点x2=e.X;
点y2=e.Y;
g、 抽绳(黑色笔,新点(startX,startY),新点(pointX2,pointY2));
j++;
}
}
}
私有无效图片box1u MouseUp(对象发送器,MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
{
g=图形。FromImage(图像);

如果(i在表单中添加
this.KeyPreview=true;
InitializeComponent()下;
调用:

public Form1()
{
    InitializeComponent();
    this.KeyPreview = true;
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
Form.KeyPreview
是一个布尔字段/属性,指示
表单
对象能够在进入焦点
控件
元素之前预览按下的键

编辑:

为了能够删除正在绘制的线条,您必须修改代码中的所有内容。但从一开始,您就应该创建一种名为
Line
的结构来存储有关绘图项的信息:

public struct Line
{
    public Point StartingPoint;
    public Point EndingPoint;
}
然后在
表单1
代码中,应删除以下字段:

int originalX, originalY;
int startX, startY, endX, endY;
int pointX1, pointY1;
int pointX2, pointY2;
Graphics g;
并将其替换为:

List<Line> lines;
Line current;
这样,您就有了一个对要创建的线的引用,并且有一个对象将保存当前正在生成的线

接下来,您直接在图像上绘制。我建议将原始图像存储为参考,并修改图像副本,因为您需要将新字段添加到
表单1

Image originalImage;
然后在
按钮2\u中单击

originalImage = Image.FromFile(@"C:\Users\student03\Documents\image\Sky2.jpg");
pictureBox1.Image = originalImage.Clone();
继续绘图部分。将
pictureBox1\u MouseDown
的内容修改为如下内容:

if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
    if (i == 0)
    {
        current = new Line();
        current.StartingPoint = new Point(e.X, e.Y);
    }
}
现在,您的当前行已填充了开始位置,您可以继续修改
pictureBox1\u MouseUp

if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
    current.EndingPoint = new Point(e.X, e.Y);
    lines.Add(current)
    pictureBox1.Image = originalImage.Clone();
    Graphics g = Graphics.FromImage(pictureBox1.Image);
    if (i <100000000 )
    {
        foreach(Line l in lines)
        {
            g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
        }     
    }
}
i++;
然后,要删除行,只需删除行集合中的最后一项并重新绘制图像:

if (e.KeyCode == Keys.Back)
{ 
    if ( lines.RemoveAt(lines.Count - 1) )
    {
        pictureBox1.Image = originalImage.Clone();
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        foreach(Line l in lines)
        {
            g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
        }  
    }
}

尽管它可以工作,但效率很低,因为您是在图像本身上绘制的,而不是在
pictureBox1
控件中。我建议您使用
pictureBox1.Paint
事件来在图像顶部绘制。

天哪,谢谢!它工作得很好!但是我似乎找不到使用退格和删除的方法我画的线条。你也能帮我吗?@VernonPichu目前几乎不可能,因为你没有在屏幕之外的任何地方存储绘图信息。但是我稍后会更新这个答案,告诉你应该做什么来删除线条。非常感谢。是的,我知道我没有在dra上存储任何数据wing.Do update me,这对我来说真是一个很大的帮助,谢谢。@VernonPichu我已经添加了代码来存储/删除线条。以后如果您需要,我可以更新它以使用有效的绘图方法。
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
    pictureBox1.Image = originalImage.Clone();
    Graphics g = Graphics.FromImage(pictureBox1.Image);
    if (i <100000000 )
    {
        foreach(Line l in lines)
        {
            g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
        }     
    }
    current.EndingPoint = new Point(e.X, e.Y);
    g.DrawLine(Pens.Black, current.StartingPoint, current.EndingPoint);
}
i++;
if (e.KeyCode == Keys.Back)
{ 
    if ( lines.RemoveAt(lines.Count - 1) )
    {
        pictureBox1.Image = originalImage.Clone();
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        foreach(Line l in lines)
        {
            g.DrawLine(Pens.Black, l.StartingPoint, l.EndingPoint);
        }  
    }
}