public static Bitmap load(string path) { Bitmap bmp = null; //file *.hdr if (path.ToLower().EndsWith(".hdr")) { FIBITMAP hdr = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_HDR, path, FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY); bmp = FreeImage.GetBitmap(FreeImage.ToneMapping(hdr, FREE_IMAGE_TMO.FITMO_DRAGO03, 2.2, 0)); FreeImage.Unload(hdr); } //file *.exr else if (path.ToLower().EndsWith(".exr")) { FIBITMAP exr = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_EXR, path, FREE_IMAGE_LOAD_FLAGS.RAW_DISPLAY); bmp = FreeImage.GetBitmap(FreeImage.ToneMapping(exr, FREE_IMAGE_TMO.FITMO_DRAGO03, 2.2, 0)); FreeImage.Unload(exr); } //file *.svg else if (path.ToLower().EndsWith(".svg")) { SvgDocument svg = SvgDocument.Open(path); bmp = svg.Draw(); } //TARGA file *.tga else if (path.ToLower().EndsWith(".tga")) { using (Paloma.TargaImage tar = new Paloma.TargaImage(path)) { bmp = new Bitmap(tar.Image); } } //PHOTOSHOP file *.PSD else if (path.ToLower().EndsWith(".psd")) { System.Drawing.PSD.PsdFile psd = (new System.Drawing.PSD.PsdFile()).Load(path); bmp = System.Drawing.PSD.ImageDecoder.DecodeImage(psd); } else { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { bmp = new Bitmap(fs, true); //GIF file *.gif if (bmp.RawFormat.Equals(ImageFormat.Gif)) { bmp = new Bitmap(path); } //ICON file *.ico else if (bmp.RawFormat.Equals(ImageFormat.Icon)) { bmp = ReadIconFile(path); } else if (bmp.RawFormat.Equals(ImageFormat.Jpeg)) { //read Exif rotation int rotation = GetRotation(bmp); if (rotation != 0) { bmp = ScaleDownRotateBitmap(bmp, 1.0f, rotation); } } } } return(bmp); }
public static Bitmap FromPath(string path) { if (File.Exists(path)) { byte[] buffer = null; MemoryStream stream = null; Bitmap bitmap = null; try { using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { buffer = new byte[fs.Length]; stream = new MemoryStream(buffer); fs.CopyTo(stream); stream.Seek(0, SeekOrigin.Begin); } var imageType = ImageUtils.GetImageType(buffer); switch (imageType) { case ImageType.PNG: bitmap = new Bitmap(stream); break; case ImageType.WEBP: using (var webp = new WebPWrapper.WebP()) { bitmap = webp.Decode(buffer); } break; case ImageType.SVG: bitmap = SvgDocument.Open <SvgDocument>(stream).Draw(); break; case ImageType.PSD: var psdFile = new System.Drawing.PSD.PsdFile(); psdFile.Load(path); bitmap = System.Drawing.PSD.ImageDecoder.DecodeImage(psdFile); break; case ImageType.ICO: using (var icon = new Icon(path)) { bitmap = icon.ToBitmap(); } break; case ImageType.TGA: using (var reader = new BinaryReader(stream)) { var image = new TgaLib.TgaImage(reader); bitmap = image.GetBitmap().ToBitmap(); } break; default: bitmap = new Bitmap(stream); break; } return(bitmap); } catch (Exception ex) { Console.WriteLine(ex); } finally { if (stream != null) { stream.Dispose(); } } } return(null); }