【问题标题】:How to hide/show the background image of pictureBox?如何隐藏/显示图片框的背景图片?
【发布时间】:2012-07-28 10:46:54
【问题描述】:

我有一个图片框,里面有一张图片。 我想要,当我单击一个按钮时,图像应该隐藏并再次单击以显示图像。

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

pictureBox1 = null;pictureBox1.Image.Dispose(); 不起作用,它显示的是带有白色背景的大红色 x。

【问题讨论】:

    标签: c# image show-hide picturebox


    【解决方案1】:

    隐藏它:

    pictureBox.Visible = false;
    

    在点击事件中隐藏/显示它:

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

    【讨论】:

      【解决方案2】:

      要切换PictureBox 中的图像,您可以创建一个 1 像素位图,并在您想要隐藏图像时将其分配给图片框,然后再重新分配图像。我有点不清楚您问题的第二部分要问什么,除非您根据某些条件将其排除在 Paint Event 中,否则图片框的 Paint Event 中的任何绘图都将保留。如果您想在框中画一条线并通过按钮打开/关闭它,请参阅我的第二个示例。

      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);
          }
      }
      

      【讨论】:

        【解决方案3】:

        picturebox1.BackgroundImage = null

        【讨论】:

        • 你可能想先用pictureBox1.Dispose()处理它
        猜你喜欢
        • 2023-04-03
        • 1970-01-01
        • 2013-08-16
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        • 1970-01-01
        • 2020-09-11
        相关资源
        最近更新 更多