如何在C#中清除pictureBox?我尝试将属性设置为null,但不起作用

时间:2019-01-23 01:45:38

标签: c#

我想在单击“编码”按钮以将其另存为新图像文件后清除pictureBox。用户保存图像文件后,我想清除表单上的字段。

我用来在pictureBox上显示图像的代码:

private void btnOpenfile_Click(object sender, EventArgs e)
    {
        // open file dialog   
        OpenFileDialog open = new OpenFileDialog();
        // image filters  
        open.Filter = "Image Files (*.png)|*.png";
        if (open.ShowDialog() == DialogResult.OK)
        {
            // display image in picture box  
            pictureBox1.Image = new Bitmap(open.FileName);
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            tbFilepath.Text = open.FileName;
            //pictureBox1.ImageLocation = tbFilepath.Text;
        }
    }

我用来清除pictureBox的代码:

private void clearForm()
    {
        pictureBox1.Image = null; //doesn't work
        pictureBox1.Invalidate(); //doesn't work 
        tbFilepath.Text = "";
        tbMessage.Text = "";
    }

我也尝试了以下方法,但也没有用:

private void clearForm()
    {
        Bitmap bm = new Bitmap(img);              
        bm.Save(tbFilepath.Text,System.Drawing.Imaging.ImageFormat.Png);
    }

我尝试使用Refresh()方法作为注释者建议的一种,但是它也不起作用:

private void clearForm()
    {
        Refresh(); //first attempt
        pictureBox1.Refresh();// second attempt
    }

我希望pictureBox字段清除我选择的现有图像,但是该图像没有清除。

点击编码按钮

Before clicking on encode button

单击“编码”按钮后,将清除textBox字段,但不会清除pictureBox字段。我使用了我在此问题中添加的代码。

after clicking on encode button

1 个答案:

答案 0 :(得分:2)

尝试创建新的位图变量并将其用于图片框:

class RcsThreadQueryHelper {
    static final String ASCENDING = "ASCENDING";
    static final String DESCENDING = "DESCENDING";
    static final String THREAD_ID = "_id";
    static final Uri THREADS_URI = Uri.parse("content://rcs/thread");
    static final Uri PARTICIPANTS_URI = Uri.parse("content://rcs/participant");
    static final String[] THREAD_PROJECTION = new String[]{THREAD_ID};
    static String buildWhereClause(RcsThreadQueryParameters queryParameters) {
        // TODO - implement
        return null;
    }
}
  

此外,尝试为位图声明全局变量,以便在将其设置为PictureBox的图像之前将其设置为图片,并将其清除。

另一方面,您可以尝试使用Picurebox的OnPaint方法:

Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
pictureBox.Image = bitmap;
pictrueBox.Invalidate();

我编写的这些代码非常完美。因此,请尝试一下。

enter image description here