depressedGirl
depressedGirl

Reputation: 127

How to clear a pictureBox in C#? I have tried setting the property to null but it doesnt work

I would like to clear a pictureBox after clicking on an 'Encode' button to save it as a new image file in my project. I would like to clear the fields on the form after the user saves the image file.

The code I use to display image on 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;
        }
    }

The code I used to clear the pictureBox:

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

I have also tried the following but it did not work either:

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

I tried using the Refresh() method as one of the commentors suggested, but it did not work either:

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

I expect the pictureBox field to clear out the existing image I have selected but the image did not clear away.

Before clicking on encode button

Before clicking on encode button

After clicking on encode button, the textBox fields are cleared but not the pictureBox field. I used the codes I have added in this question.

after clicking on encode button

Upvotes: 1

Views: 1961

Answers (1)

TerribleDog
TerribleDog

Reputation: 1247

Try creating a new Bitmap Variable and use it for the picturebox:

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

Also, try to declare a Global Variable for the Bitmap so it is the one to be set as the picture and also the one to be cleared before setting it as the image for the PictureBox.

On the other hand you could try using the OnPaint Method of the Picurebox:

   public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    }

    Bitmap bm = null;

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        pictureBox1.Image = bm;
    }

    private void btn_Open_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp); *.PNG|*.jpg; *.jpeg; *.gif; *.bmp; *.PNG";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
             bm = new Bitmap(Image.FromFile(ofd.FileName), new Size(pictureBox1.Width, pictureBox1.Height));
            textBox1.Text = ofd.FileName;
            pictureBox1.Invalidate();
        }
    }

    private void btn_Clear_Click(object sender, EventArgs e)
    {
        bm = null;
        pictureBox1.Invalidate();
    }

These code I made worked perfectly. So please try it.

enter image description here

Upvotes: 2

Related Questions