Example #1
0
        /// <summary>
        /// Loads an image.
        /// </summary>
        /// <param name="path"> The path to the image. </param>
        /// <returns> A bitmap object if the image is loaded, otherwise null. </returns>
        public static Bitmap LoadImageAsBitmap(string path, bool showError = false)
        {
            if (string.IsNullOrEmpty(path) || !File.Exists(path))
            {
                return(null);
            }

            ImgFormat fmt = GetImageFormat(path);

            if (fmt == ImgFormat.nil)
            {
                fmt = GetImageFormatFromPath(path);
            }

            try
            {
                Bitmap result;

                switch (fmt)
                {
                case ImgFormat.png:
                    result     = PNG.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.png;
                    return(result);

                case ImgFormat.bmp:
                    result     = BMP.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.bmp;
                    return(result);

                case ImgFormat.gif:
                    result     = Gif.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.gif;
                    return(result);

                case ImgFormat.jpg:
                    result     = JPEG.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.jpg;
                    return(result);

                case ImgFormat.tif:
                    result     = TIFF.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.tif;
                    return(result);

                case ImgFormat.webp:
                    result     = Webp.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.webp;
                    return(result);

                case ImgFormat.wrm:
                    result     = WORM.FromFileAsBitmap(path);
                    result.Tag = ImgFormat.wrm;
                    return(result);
                }
            }
            catch (Exception e)
            {
                if (showError)
                {
                    e.ShowError();
                }
            }
            return(null);
        }