清除picturebox上的图像。

50

我该如何在picturebox上清除绘制的图像?以下方法对我不起作用:

pictbox.Image = null;
pictbox.Invalidate();

请帮忙。

编辑

private void pictbox_Paint(object sender, PaintEventArgs e) 
{ 
     Graphics g = e.Graphics; 
     vl.Draw(g, ref tran.realListForInsert); 
} 

public void Draw(Graphics g, ref List<double> arr) 
{ 
    g.DrawRectangle(new Pen(Brushes.Red, 3), nodeArr[Convert.ToInt32(templstName)].pict.Location.X, nodeArr[Convert.ToInt32(templstName)].pict.Location.Y, 25, 25); 
    g.DrawRectangle(new Pen(Brushes.Green, 3), nodeArr[Convert.ToInt32(templstArgName)].pict.Location.X, nodeArr[Convert.ToInt32(templstArgName)].pict.Location.Y, 25, 25); 
    nodeArr[Convert.ToInt32(templstName)].text.Text = arr[Convert.ToInt32(templstArgName)].ToString(); 
    arr[Convert.ToInt32(templstName)] = arr[Convert.ToInt32(templstArgName)]; 
} 
14个回答

44

Image属性设置为null就可以了。这将清除当前在图片框中显示的任何图像。确保你的代码与以下代码完全相同:

picBox.Image = null;

37

正如其他人所说,将Image属性设置为null应该可以解决问题。

如果不行,可能是因为你使用了InitialImage属性来显示图像。如果确实是这种情况,请尝试将该属性改为null

pictBox.InitialImage = null;

根据其他答案的评论,看起来这可能是正确的解决方案。我绝对想象不出这个属性在WinForms应用程序中如何有任何作用。 - Cody Gray
@Cody,InitialImage属性保存的是在实际图像加载(通常是通过ImageLocation属性)期间显示的图像。您可以将其视为临时占位符。我同意您在@V4Vendetta答案中的评论,似乎问题提问者的PictureBox实际上并不包含图像,但看起来他确实想要删除占位符,这可能是不可能的。 - Frédéric Hamidi
不幸的是,它并不总是对我起作用。下面我展示了如何清除图片框。 - dmihailescu
将图像设置为null对我很有用。我们使用一个位图的InitialImage,上面写着“未加载图片”,这样我们就可以知道是否已经加载了图片,正在尝试加载或者已经损坏。 - Josh P

18
if (pictureBox1.Image != null)
{
    pictureBox1.Image.Dispose();
    pictureBox1.Image = null;
}

8

您需要以下内容:

pictbox.Image = null;
pictbox.update();

5
我假设你想清除通过PictureBox绘制的图片。
你可以使用Bitmap对象和Graphics对象来完成此操作。你可能在做以下操作:
Graphics graphic = Graphics.FromImage(pictbox.Image);
graphic.Clear(Color.Red) //Color to fill the background and reset the box

这是你要找的内容吗?

编辑

由于您正在使用绘图方法,这将导致每次都重新绘制,我建议在表单级别设置一个标志,指示是否应该或不应该绘制Picturebox。

private bool _shouldDraw = true;
public bool ShouldDraw
{
    get { return _shouldDraw; }
    set { _shouldDraw = value; }
}

在你的画板中,只需使用以下代码:

if(ShouldDraw)
  //do your stuff

当您点击按钮时,请将此属性设置为false,然后您应该没问题了。

哦,首先你不能通过PictureBox绘制图像,所以这没有太多意义。你只是用红色填充了图片框显示的图像。如果你想要完全删除图像,你需要将Image属性设置为null,就像我在我的答案中解释的那样。 - Cody Gray
@cody 好的,他提到了清除 绘制图像,所以我假设他正在使用图形对象来操作图像并对其执行某些操作。 - V4Vendetta
如果 pictBox.Image 没有设置为图像,则上述代码将产生 ArgumentNullException。也就是说,如果您将其设置为 null,则无法从 null 创建 Graphics 对象。 - Cody Gray
@ktarik:所以你需要通过将InitialImage属性设置为null来清除它。详情请参考Frederic的答案。 - Cody Gray
1
@ktarik:是什么让你确信你的图片框中确实显示了一张图片? - Cody Gray
显示剩余10条评论

5
private void ClearBtn_Click(object sender, EventArgs e)
{
    Studentpicture.Image = null;
}

1

为了更好地理解:

根据您的目标不同,需要记住开发人员负责处理所有不再使用或不必要的内容。

这意味着:每当不再需要时,您创建的所有内容以及pictureBox(例如:Graphics、List等)都应该被清除。

例如:假设您已将图像文件加载到PictureBox中,并希望以某种方式删除该文件。如果您没有正确卸载PictureBox中的图像文件,则无法删除该文件,因为这可能会引发异常,指出该文件正在使用中。

因此,您需要执行以下操作:

pic_PhotoDisplay.Image.Dispose();
pic_PhotoDisplay.Image = null;
pic_PhotoDisplay.ImageLocation = null;
// Required if you've drawn something in the PictureBox. Just Don't forget to Dispose Graphic.
pic_PhotoDisplay.Update();

// Depending on your approach; Dispose the Graphics with Something Like:
gfx = null;
gfx.Clear();
gfx.Dispose();

希望这能对你有所帮助。

0

我也遇到过一个顽固的图像,即使将Image和InitialImage设置为null,它也无法消失。为了永久地将图像从pictureBox中移除,我不得不使用下面的代码,通过反复调用Application.DoEvents():

            Application.DoEvents();
            if (_pictureBox.Image != null)
                _pictureBox.Image.Dispose();
            _pictureBox.Image = null;
            Application.DoEvents();
            if (_pictureBox.InitialImage != null)
                _pictureBox.InitialImage.Dispose();
            _pictureBox.InitialImage = null;
            _pictureBox.Update();
            Application.DoEvents();
            _pictureBox.Refresh();

这里完全不需要使用Application.DoEvents()。而且你还在重复运行相同的命令,这有什么意义呢? - Mark

0
我不得不在 Image = null 之后加入 Refresh() 语句才能使事情正常运行。

0
pictureBox1.Image= new Bitmap(pictureBox1.Width, pictureBox1.Height);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接