Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Free file locked by new Bitmap(filePath)

I have the Image of a PictureBox pointing to a certain file "A". At execution time I want to change the Image of the PictureBox to a different one "B" but I get the following error:

"A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file "A" because it is being used by another process."

I'm setting the Image as follows:

pbAvatar.Image = new Bitmap(filePath); 

How can I unlock the first file?

like image 432
MrCatacroquer Avatar asked Jan 26 '11 11:01

MrCatacroquer


2 Answers

Here is my approach to opening an image without locking the file...

public static Image FromFile(string path) {     var bytes = File.ReadAllBytes(path);     var ms = new MemoryStream(bytes);     var img = Image.FromStream(ms);     return img; } 

UPDATE: I did some perf tests to see which method was the fastest. I compared it to @net_progs "copy from bitmap" answer (which seems to be the closest to correct, though does have some issues). I loaded the image 10000 times for each method and calculated the average time per image. Here are the results:

Loading from bytes: ~0.26 ms per image. Copying from bitmap: ~0.50 ms per image. 

The results seem to make sense since you have to create the image twice using the copy from bitmap method.

UPDATE: if you need a BitMap you can do:

return (Bitmap)Image.FromStream(ms); 
like image 196
Brian Avatar answered Oct 07 '22 21:10

Brian


This is a common locking question widely discussed over the web.

The suggested trick with stream will not work, actually it works initially, but causes problems later. For example, it will load the image and the file will remain unlocked, but if you try to save the loaded image via Save() method, it will throw a generic GDI+ exception.

Next, the way with per pixel replication doesn't seem to be solid, at least it is noisy.

What I found working is described here: http://www.eggheadcafe.com/microsoft/Csharp/35017279/imagefromfile--locks-file.aspx

This is how the image should be loaded:

Image img; using (var bmpTemp = new Bitmap("image_file_path")) {     img = new Bitmap(bmpTemp); } 

I was looking for a solution to this problem and this method works fine for me so far, so I decided to describe it, since I found that many people advise the incorrect stream approach here and over the web.

like image 22
net_prog Avatar answered Oct 07 '22 23:10

net_prog