Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 private Image DecodeWebPBitmap(Stream stream)
 {
     using (WebPWrapper.WebP decoder = new WebPWrapper.WebP())
         return(decoder.Decode(stream.ToArray()));
 }