3

I'm making an application like a desktop. I'm trying to have a background image for the desktop, but im received an error.

Below is the button to choose a background image

private void button1_Click_2(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK)
    {
        Bitmap bit = new Bitmap(open.FileName);
        pictureBox1.Image = bit;
        pictureBox1.Image.Save(@"frame.jpeg", ImageFormat.Jpeg);
    }
}

And this is how I save it when you close and reopen the app (On Form1 Void)

if (File.Exists(@"frame.jpeg"))
    pictureBox1.Image = Image.FromFile(@"frame.jpeg");
else
    pictureBox1.BackColor = Color.Black; //Blank

I'm getting an error on the line pictureBox1.Image.Save(@"frame_backup.jpeg", ImageFormat.Jpeg);, where it says the image is in use.

I tried doing PictureBox1.Image = null; to clear the image from picture but it still errors!

Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
user3273168
  • 37
  • 1
  • 6

3 Answers3

0

It's still in memory, setting the picturebox to nothing won't solve anything because it doesn't dispose it from memory. To do that, you can use the .Dispose() method.

So....

bit.Dispose();
Frank
  • 75
  • 2
  • 13
0

As others have already said you should dispose the BitMap object when you are done with it. One more cleaner way of disposing this BitMap object is to include within the using block as BitMap implements IDisposable (via Its base class Image).

using(Bitmap bit = new Bitmap(open.FileName))
{
        pictureBox1.Image = bit;
        pictureBox1.Image.Save(@"frame.jpeg", ImageFormat.Jpeg);
}

EDITED

    Bitmap bit = new Bitmap(open.FileName);
    Bitmap bitNew = new Bitmap(bit);
    bit.Dispose();
    pictureBox1.Image = bmpNew;
    pictureBox1.Image.Save(@"frame.jpeg", ImageFormat.Jpeg);
    bitNew.Dispose();
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
  • An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+. – user3273168 Feb 16 '14 at 16:12
  • May be this would help - http://stackoverflow.com/questions/5813633/a-generic-error-occurs-at-gdi-at-bitmap-save-after-using-savefiledialog – Ashish Gupta Feb 16 '14 at 16:15
  • Are you trying to say the directory doesnt exist that im trying to save in? Is there away to save it in C:\Program Files (x86) because im sure it has administration control, But its a good and safe way to say the applications files? – user3273168 Feb 16 '14 at 16:19
  • Well, first, can you post the stacktrace in the exception. Also look at the HResult/ErrorCode property in the exception. I dont think It needs to be saved in the Program Files. – Ashish Gupta Feb 16 '14 at 16:22
  • at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at Sinox_4.Desktop_Frame.button1_Click_2(Object sender, EventArgs e) in c:\Users\Harry\Documents\Visual Studio 2013\Projects\Sinox 4\Sinox 4\Desktop_Frame.cs:line 108 – user3273168 Feb 16 '14 at 16:32
  • Did you also see the HResult/ErrorCode in the ExternalException? – Ashish Gupta Feb 16 '14 at 16:39
  • Im new to C#, How would i get that? is it in a catch block? – user3273168 Feb 16 '14 at 16:43
  • Just edited my answer. Can you try the new code I added? – Ashish Gupta Feb 16 '14 at 16:50
  • I still get a error on the save line Additional information: A generic error occurred in GDI+. – user3273168 Feb 16 '14 at 17:07
  • Is there anything more you can guide me towards to fix this? – user3273168 Feb 16 '14 at 17:53
0

Form your code it looks to me that you are interested to open an image, say in PNG format and save it as JPEG format. You should try like this:

Idea behind this answer is to use using block which helps to dispose resource as soon as it is no longer in use, and release the opened resource by getting it from the owner (see ReleaseInUseResource` function).

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        if (open.ShowDialog() == DialogResult.OK)
        {
            ReleaseInUseResource();

            using (Bitmap bmp = new Bitmap(open.FileName))
            {
                bmp.Save(@"frame.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            pictureBox1.Image = Bitmap.FromFile(@"frame.jpeg");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (System.IO.File.Exists(@"frame.jpeg"))
        {
            ReleaseInUseResource();
            pictureBox1.Image = new Bitmap(@"frame.jpeg");
        }
        else
        {
            pictureBox1.BackColor = Color.Black; //Blank
        }
    }

    private void ReleaseInUseResource()
    {
        Image img = pictureBox1.Image;  // Get image used to display in picture box.
        if (img != null) img.Dispose(); // release it first if there is an image opened.
    }

Hope it helps!

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56