Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a WinForm application using VS2010 & VS2012 with .Net4.

In the application I load individual images into pictureboxes and then delete those images the user does not wish to keep.

Everytime I try to delete these "unwanted" images I get the above error message:
"The process cannot access the file xxxxxx because it is being used by another process"

The following is used to load an image into a dynamically created picturebox together with the code to delete the unwanted images.

Option 1:
Load Image:
VB
picBox.Image = Image.FromFile(imgInfo.FullName).GetThumbnailImage(128, 128, Nothing, Nothing)

Delete Image:
VB
For Each imgFile As String In Directory.GetFiles(imgSharedFolder)
   File.Delete(imgFile)
Next imgFile


Option 2:
Since loading an image file into a picturebox will "lock" the image file I tried the following by reading the image file into a FileStream and then Close the FileStream after loading the image into the picturebox

Load:
VB
fs = New System.IO.FileStream(imgInfo.FullName, IO.FileMode.Open, IO.FileAccess.Read)
picBox.Image = System.Drawing.Image.FromStream(fs).GetThumbnailImage(128, 128, Nothing, Nothing)
fs.Close()

Delete:
VB
Dim picList As String() = Directory.GetFiles(imgSharedFolder, "*.jpg")
For Each f As String In picList
   File.Delete(f)
Next f

I get the same error message.

Option 3:
After some more searching, reading and trying I came across this suggestion to create an image object and then Dispose of the image object once the image is loaded into the picturebox:

Load:
VB
Dim newImage As Image
newImage = Image.FromFile(imgInfo.FullName).GetThumbnailImage(128, 128, Nothing, Nothing)
picBox.Image = newImage


Unfortunately I just got the same error message.

Is there any other possible solution to this problem, something I may have overlooked ?
Posted
Updated 3-Jul-18 0:36am

Image.FromFile and Image.FromStream require that the file / stream are available for the life of the Image that is created from them - as a result, they hold the file handle until they Image is Disposed.

The easiest way to do it is to load the image, copy it into a new Image, and then Dispose the original. This is the code I use for C#:
C#
/// <summary>
/// Get an Image without locking the file.
/// </summary>
/// <remarks>
/// Image.FromFile and Image.FromStream have problems in that the first
/// locks the file until the Image is Disposed, and teh latter requires
/// the stream to be opena nd available until the Image is Disposed, which
/// effectively locks the file as well.
/// This doesn't, because it uses a copy of the image.
/// </remarks>
/// <param name="path"></param>
/// <returns></returns>
public static Image GetImage(string path)
    {
    Image img;
    using (Image temp = Image.FromFile(path))
        {
        img = new Bitmap(temp);
        }
    return img;
    }

Unfortunately the online translator I use isn't responding at the moment, so...
VB
Dim img As Image
Dim tmp as Image = Image.FromFile(path)
img = new BitMap(tmp)
tmp.Dispose()
Should do it, but it's not tested.
 
Share this answer
 
Comments
Tino Fourie 2-Jun-14 14:25pm    
OG, Option 3 of the code I posted is touching on what you have given but is not quiet the same as yours. I will definately try your code (even the translated code looks pretty good). I will post back in a minute !!
Tino Fourie 2-Jun-14 14:37pm    
Unfortunately, that is a no can do mate. What I do find interesting is that out of the 7 image files I load only the last loaded image produces the error message.

I have tried all sorts of possibilities in both VS2010 & VS2012 on both Vista and Win 7 and it is always the last image files read that is locked.

Is there perhaps another way of applying the same code but in another way of logic ?
Tino Fourie 2-Jun-14 14:50pm    
I am currently studying a post on StackOverFlow which is very comprehensive and detailed with regards to "locked" image files.
Here is the link (http://stackoverflow.com/questions/18250848/how-to-prevent-the-image-fromfile-method-to-lock-the-file) for those of you who are also stuck on a similar issue.
I had a similar issue to this where I needed to delete an image file after displaying the image in a picturebox control.
It always worked with framework 3.5 then became an issue with framework 4.0
We resolved this by forcing the garbage collection after setting the picturebox image to nothing
Example:
picPhoto.Image = Nothing
picPhoto.Refresh()
GC.Collect()
 
Share this answer
 
Comments
Richard Deeming 3-Jul-18 9:04am    
FOUR YEARS too late. And that issue has been around since .NET 1.0:
Image file is locked when you set the PictureBox Image property to a file[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900