Opens an image without locking the file, returns the image if loaded and null if failure. - CSharp System.Drawing

CSharp examples for System.Drawing:Image Effect

Description

Opens an image without locking the file, returns the image if loaded and null if failure.

Demo Code


using System.IO;/*from ww w  . jav a2 s .c om*/
using System.Drawing;

public class Main{
        #region Public Methods and Operators

        /// <summary>
        /// Opens an image without locking the file, returns the image if loaded and null if failure.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// Image.
        /// </returns>
        /// <remarks>
        /// SOURCE: http://stackoverflow.com/questions/4803935/free-file-locked-by-new-bitmapfilepath/8701748#8701748
        /// </remarks>
        public static Image FromFile(string path)
        {
            byte[] bytes = File.ReadAllBytes(path);
            var ms = new MemoryStream(bytes);
            Image img = Image.FromStream(ms);
            return img;
        }
}

Related Tutorials