private static Image LoadImagePriv(Stream s) { // Image.FromStream wants the stream to be open during // the whole lifetime of the image; as we can't guarantee // this, we make a copy of the image Image imgSrc = null; try { #if NETSTANDARD2_0 using (var managedStream = new SkiaSharp.SKManagedStream(s)) { // SkiaSharp.SKPixmap pxMap = new SkiaSharp.SKPixmap() using (var original = SkiaSharp.SKBitmap.Decode(managedStream)) { return(SkiaSharp.SKImage.FromBitmap(original)); } } #elif !KeePassLibSD imgSrc = Image.FromStream(s); NormalizeOrientation(imgSrc); Bitmap bmp = new Bitmap(imgSrc.Width, imgSrc.Height, PixelFormat.Format32bppArgb); try { bmp.SetResolution(imgSrc.HorizontalResolution, imgSrc.VerticalResolution); Debug.Assert(bmp.Size == imgSrc.Size); } catch (Exception) { Debug.Assert(false); } #else imgSrc = new Bitmap(s); Bitmap bmp = new Bitmap(imgSrc.Width, imgSrc.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.Transparent); #if !KeePassLibSD g.DrawImageUnscaled(imgSrc, 0, 0); #else g.DrawImage(imgSrc, 0, 0); #endif } return(bmp); #endif } finally { if (imgSrc != null) { imgSrc.Dispose(); } } }
public static Image LoadImage(byte[] pb) { if (pb == null) { throw new ArgumentNullException("pb"); } MemoryStream ms = new MemoryStream(pb, false); try { return(Image.FromStream(ms)); } finally { ms.Close(); } }