[Mono-devel-list] System.ArgumentException when using System.Drawing.Image.FromFile
Jonathan Gilbert
2a5gjx302 at sneakemail.com
Tue Apr 13 19:55:37 EDT 2004
At 11:31 AM 12/04/2004 -0700, you wrote:
[snip]
>Code
>--
[snip]
> Bitmap picture = new Bitmap(Image.FromFile("test.jpg",
true));
> Image img = Image.FromFile("test.jpg");
[snip]
Just thought I'd point out that Image.FromFile() should be avoided. Under
the MS runtime, it leaves a lock on the file until the bitmap object is
disposed; if you try to Image.FromFile the same file twice, you will get an
IO exception about the file being locked. In addition, it can't be used by
other applications during this time. This is a known issue that is
documented in the MS knowledge-base. I'm not sure if Mono is duplicating
this behaviour, but I should point out that the knowledge-base article
states that "This behaviour is by design."
http://support.microsoft.com/default.aspx?scid=kb;en-us;311754
The canonical work-around is to use Image.FromStream. To be fully
resource-safe, you need something like this:
FileStream stream = null;
Bitmap bmp = null;
try
{
stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read);
bmp = (Bitmap)Bitmap.FromStream(stream);
}
finally
{
if (stream != null)
stream.Close();
}
Jonathan
More information about the Mono-devel-list
mailing list