[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:
>                Bitmap picture = new Bitmap(Image.FromFile("test.jpg",
>                Image img = Image.FromFile("test.jpg");

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."


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;

  stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read);
  bmp = (Bitmap)Bitmap.FromStream(stream);
  if (stream != null)


More information about the Mono-devel-list mailing list