File lock problem with Image.FromFile()
-
Hi, when I load an image using this
Bitmap bitmap = (Bitmap)Image.FromFile(pic.Path)
, it is not possible to delete the file afterwards, unless you exit the program. I tried to call.Dispose()
on the object, but it seems like a bug in theFromFile
method. I found out a workaroundBitmap origbmp = new Bitmap(pic.Path); Bitmap bitmap = new Bitmap(origbmp); origbmp.Dispose();
. I keep working with the copy of the original bitmap as it's not locked anymore. But, loading an image usingBitmap
's constructor, it's impossible to load animated GIFs as it seems thatBitmap
doesn't support them. Now, how can I use animated GIFs and other image formats AND be able to delete the file afterwards? Thx :-D Stefan -
Hi, when I load an image using this
Bitmap bitmap = (Bitmap)Image.FromFile(pic.Path)
, it is not possible to delete the file afterwards, unless you exit the program. I tried to call.Dispose()
on the object, but it seems like a bug in theFromFile
method. I found out a workaroundBitmap origbmp = new Bitmap(pic.Path); Bitmap bitmap = new Bitmap(origbmp); origbmp.Dispose();
. I keep working with the copy of the original bitmap as it's not locked anymore. But, loading an image usingBitmap
's constructor, it's impossible to load animated GIFs as it seems thatBitmap
doesn't support them. Now, how can I use animated GIFs and other image formats AND be able to delete the file afterwards? Thx :-D Stefan -
Hi, when I load an image using this
Bitmap bitmap = (Bitmap)Image.FromFile(pic.Path)
, it is not possible to delete the file afterwards, unless you exit the program. I tried to call.Dispose()
on the object, but it seems like a bug in theFromFile
method. I found out a workaroundBitmap origbmp = new Bitmap(pic.Path); Bitmap bitmap = new Bitmap(origbmp); origbmp.Dispose();
. I keep working with the copy of the original bitmap as it's not locked anymore. But, loading an image usingBitmap
's constructor, it's impossible to load animated GIFs as it seems thatBitmap
doesn't support them. Now, how can I use animated GIFs and other image formats AND be able to delete the file afterwards? Thx :-D StefanDon't pat yourself on the back too hard. This has been known for at least 4 years now, and it's not exactly a bug. Though, some people would probably argue that it is. An eaiser method would be to load the bitmap from a FileStream instead of creating two Bitmaps to get one. You can read more about it in this[^] KB article on MSDN. Dave Kreskowiak Microsoft MVP - Visual Basic
-
Don't pat yourself on the back too hard. This has been known for at least 4 years now, and it's not exactly a bug. Though, some people would probably argue that it is. An eaiser method would be to load the bitmap from a FileStream instead of creating two Bitmaps to get one. You can read more about it in this[^] KB article on MSDN. Dave Kreskowiak Microsoft MVP - Visual Basic
Dave Kreskowiak wrote:
This has been known for at least 4 years now, and it's not exactly a bug. Though, some people would probably argue that it is.
When you call .Dispose() and the handle won't get free, it's a bug!
Dave Kreskowiak wrote:
You can read more about it in this[^] KB article on MSDN.
Thank you, that worked! :) Regards, Stefan