/// <summary> /// Load an image from a file ensuring file is not left locked /// </summary> /// <param name="fileName">File specification (path and filename)</param> /// <returns>image as Bitmap</returns> public static Bitmap LoadImageFromFile(string fileName) { Graphics g; Bitmap sourceImage = null; System.IO.FileStream fs = null; try { // This is a long work around for a bug in VS2005 and VS2003 // that would cause GDI errors if you did a new Bitmap from a Bitmap Bitmap tempImage = new Bitmap(fileName); sourceImage = new Bitmap(tempImage.Size.Width, tempImage.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); sourceImage.SetResolution(tempImage.HorizontalResolution, tempImage.VerticalResolution); g = Graphics.FromImage(sourceImage); g.DrawImage(tempImage, 0, 0); tempImage.Dispose(); g.Dispose(); // Specify a valid picture file path on your computer. Using this process does not leave image file locked //fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); //sourceImage = (Bitmap)System.Drawing.Image.FromStream(fs); } catch (Exception ex) { ErrorMessage.Display(ex, "frmImageColorRGB(fileName)"); } finally { if (fs != null) { fs.Close(); fs.Dispose(); } } return(sourceImage); }