public static Image getImageFromID(string pId) { if (_imageCache.ContainsKey(pId)) { Log.Info("Using cached image for Design ID '" + pId + "'"); return(_imageCache[pId]); } string path = getPath(pId); if (String.IsNullOrWhiteSpace(path)) { Log.Error("Design ID '" + pId + "' not found."); return(null); } if (!System.IO.File.Exists(path)) { Log.Error("DesignID file at: '" + path + "' does not exist."); return(null); } else { Log.Info("Loading DesignID: " + path.Split("/").Last()); } if (path.EndsWith(".webp")) { // load webp try { WebPWrapper.WebP webP = new WebPWrapper.WebP(); Image bm = webP.Load(path); addToImageCache(pId, bm); return(bm); } catch (Exception e) { Log.Error("Loading '" + path + "' failed with error: '" + e.Message + "'."); return(null); } } else { // load image try { Image bm = Image.FromFile(path); addToImageCache(pId, bm); return(bm); } catch (Exception e) { Log.Error("Loading '" + path + "' failed with error: '" + e.Message + "'."); return(null); } } }
private Image DecodeWebPBitmap(Stream stream) { using (WebPWrapper.WebP decoder = new WebPWrapper.WebP()) return(decoder.Decode(stream.ToArray())); }
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); }
private void EncodeWebPBitmap(Image image, Stream stream, IImageEncoderOptions encoderOptions) { using (WebPWrapper.WebP encoder = new WebPWrapper.WebP()) using (MemoryStream webPStream = new MemoryStream(encoder.EncodeLossy(image as Bitmap, encoderOptions.Quality))) webPStream.CopyTo(stream); }