2012-07-28 117 views
1

我有一个pictureBox里面的图像。 我想,当我点击一个按钮时,图像应该隐藏,然后再次点击以显示图像。如何隐藏/显示pictureBox的背景图片?

在pictureBox中,使用绘画事件我画了一些线条。 所以如果我在做pictureBox1.Refresh();它会画线。我想,如果我点击一个按钮,图像将不会显示/关闭。

pictureBox1 = null;pictureBox1.Image.Dispose();不起作用它显示我大红色x与白色背景。

回答

0

对于切换图像您PictureBox您可以创建一个1个像素的位图,并将其分配给想要隐藏图像的图片框,然后重新指定图像。我有点不清楚问题的第二部分是什么问题,除非您在基于某些条件的Paint Event中排除它,否则该图片框的Paint事件中的任何绘图将保留。如果你想在框中画一条线,可以从一个按钮上打开/关闭,看看我的第二个例子。

public partial class Form1 : Form 
{ 
    Bitmap nullBitmap = new Bitmap(1, 1); // create a 1 pixel bitmap 
    Bitmap myImage = new Bitmap("Load your Image Here"); // Load your image 
    bool showImage; // boolean variable so we know what image is assigned 
    public Form1() 
    { 
     InitializeComponent(); 
     pictureBox1.Image = myImage; 
     showImage = true; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (showImage) 
     { 
      pictureBox1.Image = nullBitmap; 
      showImage = false; 
     } 
     else 
     { 
      pictureBox1.Image = myImage; 
      showImage = true; 
     } 
    } 
} 

第二示例

public partial class Form1 : Form 
{ 
    bool showLines; 
    public Form1() 
    { 
     InitializeComponent(); 
     showLines = true; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (showLines) 
     { 
      showLines = false; 
      pictureBox1.Invalidate(); 
     } 
     else 
     { 
      showLines = true; 
      pictureBox1.Invalidate(); 
     } 
    } 

    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     if(showLines) 
      e.Graphics.DrawLine(Pens.Purple, 0, 0, 100, 100); 
    } 
} 
1

来隐藏它:

pictureBox.Visible = false; 

要隐藏/显示它的单击事件:

void SomeButton_Click(Object sender, EventArgs e) 
{ 
    pictureBox.Visible = !pictureBox.Visible; 
}