2006-06-12
Reading an image from a file without locking it
When you use Image.FromFile to read an image from a file, the file is locked. The following method reads an image from a file without locking it:
public static class Utils { public static Image ImageFromFile(string fileName) { if (fileName == null) throw new ArgumentNullException("fileName"); using(FileStream lStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) return Image.FromStream(lStream); } }Here's an example how to use it:
// old method that locks the file: Image img = Image.FromFile("picture.jpg"); // new method that does not lock the file: Image img = Utils.ImageFromFile("picture.jpg");
