0

I need to move/rename some images which are loaded inside of pictureboxes, the images are stored in a folder in the executable path of the application. Before I can move/rename any image I have to make sure it is not in use, so I tried to load another image from the executable path of the application into this picturebox and also tried to "null" the picturebox image property.

If I want to move on in my program the image is still locked I verifyed this by using the following code:

protected virtual bool IsFileLocked(FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
        return false;
    }

I also tried to use the code from here: What process locks a file?

Which returned me the following: System.Collections.Generic.List´1[System.Diagnostics.Process]

I have to tell you that I am also fighting with an AccessViolationException using the same images and methods. I am not entirely sure if it is the same problem...is the way I saved the Images wrong or the way I am using them? Let me know if you need more code or Information!

EDIT:

Both the link from TaW and the answer from Anon Coward did not work for me and I really tried my hardest to make it work, I tried to dispose and null the picturebox, the image etc. .I found a solution mixed with your Suggestion but it is a very dirty one...

var image2 = Image.FromFile(infos[i].FullName);
                                MemoryStream ms = new MemoryStream();
                                if (ImageFormat.Jpeg.Equals(image2.RawFormat))
                                {
                                    // JPEG
                                    image2.Save(ms, ImageFormat.Jpeg);
                                }
                                else if (ImageFormat.Png.Equals(image2.RawFormat))
                                {
                                    // PNG
                                    image2.Save(ms, ImageFormat.Png);
                                }
                                else if (ImageFormat.Gif.Equals(image2.RawFormat))
                                {
                                    // Bitmap
                                    image2.Save(ms, ImageFormat.Bmp);
                                }
                                else if (ImageFormat.Icon.Equals(image2.RawFormat))
                                {
                                    // Icon
                                    image2.Save(ms, ImageFormat.Icon);
                                }
                                image2.Dispose();

                                System.GC.Collect();
                                System.GC.WaitForPendingFinalizers();

                                System.IO.File.Delete(infos[i].FullName);

                                Image newImg = Image.FromStream(ms);
                                newImg.Save(neuer_name2);
                                newImg = null;

I know that using GarbageCollection is not the right way, but I have no idea what blocks the images...so I am still searching for a real solution here.

Community
  • 1
  • 1
user3868224
  • 363
  • 1
  • 6
  • 19
  • 1
    The code that loads and that tries to change/move them would be even more interesting than the one that tries to unlock. Please show all.. Did you see [this](http://stackoverflow.com/questions/37736815/overwrite-image-picturebox-in-c-sharp/37741101?s=2|0.0000#37741101)? – TaW Jul 09 '16 at 18:56
  • I saw the link and I am working on it right now, if this will not help I will edit my original question. – user3868224 Jul 09 '16 at 19:01
  • See the edit on my answer, I had forgotten there's an old GDI+ issue you might be running into – Anon Coward Jul 12 '16 at 16:48

1 Answers1

3

When you set the property to null, you're only marking the object as available for garbage collection at some future point, so the file handle for the object will remain open till some future point.

If you plan to modify the file, you need to do two things: Both make the property Null so the PictureBox doesn't try to use it, as well as dispose the Image itself so the file handle is closed:

var image = pictureBox1.Image;
pictureBox1.Image = null;
image.Dispose();

Also, there is an issue with GDI+ that you might be running into. You'll need to prevent the Image constructor from keeping a reference to the file in the first place, you can easily do this by reading it into a MemoryStream first, then passing that to the constructor:

Image image = Image.FromStream(new MemoryStream(File.ReadAllBytes("test.png")))
Community
  • 1
  • 1
Anon Coward
  • 9,784
  • 3
  • 26
  • 37