图片框上的图像清晰

时间:2011-05-02 10:45:33

标签: c# .net winforms 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)]; 
} 

13 个答案:

答案 0 :(得分:35)

正如其他人所说,将Image属性设置为null应该有效。

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

pictBox.InitialImage = null;

答案 1 :(得分:28)

Image property设置为null将正常工作。它将清除图片框中当前显示的任何图像。确保您已完全按照以下方式编写代码:

picBox.Image = null;

答案 2 :(得分:9)

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

答案 3 :(得分:4)

我假设您要清除通过PictureBox绘制的图像。

这将通过Bitmap对象并使用Graphics对象实现。你可能会做类似

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

这是你在寻找什么?

修改

由于你使用的是paint方法,每次都会重新绘制它,我建议你在formlevel上设置一个标志,指示它是否应该绘制Picturebox

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

在你的油漆中使用

if(ShouldDraw)
  //do your stuff

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

答案 4 :(得分:4)

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

答案 5 :(得分:3)

您需要以下内容:

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

答案 6 :(得分: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();

答案 7 :(得分:0)

我必须在Image = null之后添加一个Refresh()语句来使事情有效。

答案 8 :(得分:0)

我使用此方法从图片框清除图像。这可能会帮助一些人

private void btnClear1_Click(object sender, EventArgs e)
        {

            img1.ImageLocation = null;
        }

答案 9 :(得分:0)

出于了解的目的:

请记住,开发人员有责任处置不再使用或不需要的所有东西,具体取决于您如何实现目标。

这意味着::您与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();

希望这对您有所帮助。

答案 10 :(得分:-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;

答案 11 :(得分:-2)

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

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

答案 12 :(得分:-2)

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