-1

I'm working on a program where you can change the wallpaper of the main form "Form1" from the "Options" form. On the Options form, there is an openfiledialog where u can select an image and it copies the image to the application directory.

    private void wallpaperBrowser_Click(object sender, EventArgs e)
    {
        wallpaperOptioncolor.Visible = false;
        OpenFileDialog of = new OpenFileDialog();
        of.Title = "Select Image";
        of.Filter = "Image Files (*.jpg;*.jpeg,*.png,*.gif)|*.JPG;*.JPEG;*.PNG;*.GIF";
        if (of.ShowDialog() == DialogResult.OK)
        {
            File.Copy(of.FileName, Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\CurrentWallpaper.png", true);

            (this.Owner as Form1).mainBackground.Image = new Bitmap(of.FileName);
            (this.Owner as Form1).BackgroundImage = new Bitmap(of.FileName);
            currentWallpaper.BackgroundImage = (this.Owner as Form1).mainBackground.Image;

            (this.Owner as Form1).BackgroundImageLayout = ImageLayout.Stretch;
            (this.Owner as Form1).mainBackground.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    }

Then on the load event on the main form, it checks if the file "CurrentWallpaper.png" exists. If it does exist it sets the wallpaper but if you try to change the wallpaper it will get with an error.

"System.IO.IOException: 'The process cannot access the file 'C:\Users\mc_jgeorgio20\Documents\DesktopReborn\CurrentWallpaper.png' because it is being used by another process.'

        if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\CurrentWallpaper.png"))
        {
            BackgroundImageLayout = ImageLayout.Stretch;
            mainBackground.SizeMode = PictureBoxSizeMode.StretchImage;

            BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\CurrentWallpaper.png");
            mainBackground.Image = BackgroundImage;
        }
        else
        {
            BackgroundImage = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\DefaultWallpaper.jpg");
        }

I've tried this many times and cannot figure out how to set the PictureBox image what am I doing wrong!

Image img;
            using (var bmpTemp = new Bitmap(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\DesktopReborn\\CurrentWallpaper.png"))
            {
                img = new Bitmap(bmpTemp);
                mainBackground.Image = img; //mainBackground is the picturebox
            }
Joseph
  • 11
  • 4
  • Possible duplicate of [Open Image from file, then release lock?](https://stackoverflow.com/questions/6576341/open-image-from-file-then-release-lock) – JuanR Nov 07 '18 at 14:39
  • @JuanR That doesn't work I've tried so many times. – Joseph Nov 07 '18 at 14:50
  • @Joseph it does work. If you use it correctly. If you have a problem with your code then create a new question with the code – slow Nov 07 '18 at 15:15
  • @sLw Can't post another question for 2 days I will edit it above. – Joseph Nov 07 '18 at 15:25
  • @Joseph: Welcome to SO. Your code should work fine. I tested it myself locally which tells me that the issue is somewhere else. You must have a piece of code somewhere doing something with the file. Maybe you open it to edit it or something of the sort. The point is a handle is being created but it is not being released (via `Dispose` method). Check the rest of your code. – JuanR Nov 07 '18 at 15:37
  • By the way, check for external applications as well (e.g. image editors such as Photoshop) that may be keeping a handle on the file. – JuanR Nov 07 '18 at 15:46
  • @JuanR I solved it thank you! :) – Joseph Nov 07 '18 at 19:51
  • @Joseph: Glad to hear that. I am curious, what was the issue? – JuanR Nov 07 '18 at 20:04
  • @JuanR the file was being locked during runtime so I couldn't delete or change during runtime. And when I used the code the guy posted below it didn't work in the Form1_Load event. So I made a handler after InitializeComponent() and it worked! – Joseph Nov 07 '18 at 20:22

1 Answers1

3

Image.FromFile will attempt to lock the file, as it says in the documentation:

"The file remains locked until the Image is disposed."

If another process already has the file locked, then Image.FromFile will throw the IOException you have seen. What you should use instead is a Bitmap, e.g.,

Image img = null;
using (Bitmap bitmap = new Bitmap(fileName))
{
    img = new Bitmap(bitmap);
}

See also: Open Image from file, then release lock?.

Polyfun
  • 9,479
  • 4
  • 31
  • 39