1

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

depressedGirl
  • 127
  • 1
  • 13
  • The `picturebox1.Image = null` should work just fine are you sure that is the name of the picture box – preciousbetine Jan 23 '19 at 01:49
  • have you tried calling `Refresh()` afterward? – nik.shornikov Jan 23 '19 at 02:03
  • Possible duplicate of [Clear image on picturebox](https://stackoverflow.com/questions/5856196/clear-image-on-picturebox) – Lance U. Matthews Jan 23 '19 at 02:06
  • @preciousbetine yes it is the name of the pictureBox, I used pictureBox1.Image = null as stated in my question but it did not work, hence I had to resort to asking here. – depressedGirl Jan 23 '19 at 03:26
  • @BACON Sorry if it is a duplicate but as you can see in my question I have already used the solution provided by the similar stackoverflow question. If that solution worked I would not have spent time to type out my question here and screenshotting evidences. – depressedGirl Jan 23 '19 at 03:26
  • @depressedGirl Have you tried calling `Refresh()` and/or `Update()` as some of the answers propose? – Lance U. Matthews Jan 23 '19 at 03:36
  • @nik.shornikov I don't have a Refresh() method. the method I am using is clearForm() – depressedGirl Jan 23 '19 at 03:49
  • @BACON I did not code a Refresh or Update method. The method I am using is clearForm() which I have already mentioned in my question – depressedGirl Jan 23 '19 at 03:50

1 Answers1

2

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

TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • Hi, I appreciate your help. I tried both your global variable methods and OnPaint methods, both did not work either. Also, I removed the pictureBox1.Invalidate(); at the open file method you provided as it makes the image not visible when I try to open an image file. – depressedGirl Jan 23 '19 at 03:48
  • @depressedGirl i'm gonna try to recreate your project, I'll get back to you. – TerribleDog Jan 23 '19 at 03:51
  • 1
    I have tried out your code, it works now! Really appreciate your time and effort to help me! – depressedGirl Jan 23 '19 at 04:16
  • @depressedGirl I modified the code in the opening of the file for the picture to adapt to the picturebox. – TerribleDog Jan 23 '19 at 05:17