Beispiel #1
0
        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(); }
        }
Beispiel #2
0
        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 !KeePassLibSD
                imgSrc = Image.FromStream(s);
                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);
#endif

                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);
            }
            finally { if (imgSrc != null)
                      {
                          imgSrc.Dispose();
                      }
            }
        }