图片框上的清晰图像

问题描述 投票:0回答:14

如何清除图片框上的绘图? 以下内容对我没有帮助:

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)]; 
} 
c# .net winforms picturebox
14个回答
44
投票

Image
属性 设置为 null 即可正常工作。它将清除图片框中当前显示的任何图像。确保您编写的代码完全像这样:

picBox.Image = null;

37
投票

正如其他人所说,将

Image
属性设置为
null
应该有效。

如果没有,则可能意味着您使用了 InitialImage 属性来显示图像。如果确实如此,请尝试将该属性设置为

null

pictBox.InitialImage = null;

17
投票
if (pictureBox1.Image != null)
{
    pictureBox1.Image.Dispose();
    pictureBox1.Image = null;
}

8
投票

您需要:

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

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

5
投票

我假设你想清除通过 PictureBox 绘制的图像。

这可以通过 Bitmap 对象和使用 Graphics 对象来实现。你可能正在做类似的事情

Graphics graphic = Graphics.FromImage(pictbox.Image);
graphic.Clear(Color.Red) //Color to fill the background and reset the box

这就是你要看的东西吗?

编辑

由于您使用的是 paint 方法,这会导致每次都重新绘制它,我建议您在表单级别设置一个标志,指示是否应该绘制 Picturebox

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

在你的油漆中只需使用

if(ShouldDraw)
  //do your stuff

当您单击按钮时,将此属性设置为 false,您应该没问题。


1
投票

为了理解:

根据您实现目标的方式,请记住,开发人员有责任处置不再使用或不再需要的所有内容。

这意味着: 您与图片框一起创建的所有内容(即:图形、列表;等)将在不再需要时被丢弃。

例如: 假设您有一个图像文件加载到您的 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();

0
投票

我必须在 Image = null 之后添加一个 Refresh() 语句才能使事情正常进行。


0
投票

我用这个方法清除了picturebox中的图片。它可能对某些人有帮助

private void btnClear1_Click(object sender, EventArgs e)
        {

            img1.ImageLocation = null;
        }

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

-1
投票

就这么简单! 您可以使用按钮单击事件, 我将它与按钮属性名称一起使用:“btnClearImage”

// Note 1a:
// after clearing the picture box 
// you can also disable clear button 
// by inserting follwoing one line of code:

btnClearImage.Enabled = false   

// Note 1b:
// you should set your button Enabled property
// to "False"
// after that you will need to Insert 
// the following line to concerned event or button
// that load your image into picturebox1 
// code line is as follows:

btnClearImage.Enabled = true;

-2
投票

你应该试试。当您清除图形时,您必须选择颜色。 SystemColors.Control 是表单的原生颜色

Graphics g = pB.CreateGraphics();
g.Clear(SystemColors.Control);

-3
投票

clear pictureBox in c# winform Application c# winform 应用程序中清空 pictureBox 的简单方法

© www.soinside.com 2019 - 2024. All rights reserved.