public Bitmap RetrieveBitmap(string key, int headerSize = 0)
        {
            ValidateKey(key);
            Argument.Assert.IsNotNegative(headerSize, nameof(headerSize));

            object result = Retrieve(key);

            if (result == null)
            {
                return(null);
            }

            Bitmap bitmap = result as Bitmap;

            if (bitmap != null)
            {
                return(bitmap);
            }

            byte[] imageBytes = result as byte[];

            if (imageBytes != null)
            {
                bitmap = MemoryBitmap.FromBuffer(imageBytes, headerSize);
            }

            return(bitmap);
        }
Exemple #2
0
        /// <summary>
        ///     Creates a new <see cref="ImageProperties" /> from the supplied <see cref="Stream" />.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A new <see cref="ImageProperties" />.</returns>
        /// <exception cref="ArgumentException">
        ///     The <paramref name="stream" /> does not
        ///     contain a valid  <see cref="Bitmap" /> object.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     The <paramref name="stream" /> is
        ///     null.
        /// </exception>
        public ImageProperties(Stream stream)
        {
            Argument.Assert.IsNotNull(stream, "stream");

            Bitmap = MemoryBitmap.FromStream(stream);
            if (Bitmap == null)
            {
                throw new ArgumentException("The stream does not contain a valid bitmap object.", "stream");
            }
        }
Exemple #3
0
 public void CreateThumbnailFromJpg()
 {
     using (Image image = Image.FromFile(@"Drawing\Resources\ULPT-L.jpg"))
         using (Image thumb = Thumbnail.FromImage(image, 134, 107))
         {
             using (MemoryStream stream = new MemoryStream())
             {
                 JpgFormat.Save(stream, thumb, 80);
                 using (Bitmap bitmap = MemoryBitmap.FromStream(stream))
                 {
                     Assert.AreEqual(new Size(134, 107), bitmap.Size);
                 }
             }
         }
 }
Exemple #4
0
        private static Bitmap CreateFromIndexed(Image image, int maxWidth, int maxHeight)
        {
            using (Bitmap dst = new Bitmap(maxWidth, maxHeight))
            {
                using (Graphics g = Graphics.FromImage(dst))
                {
                    g.SmoothingMode     = SmoothingMode.AntiAlias;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(image, 0, 0, dst.Width, dst.Height);
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    dst.Save(stream, ImageFormat.Png);
                    return(MemoryBitmap.FromStream(stream));
                }
            }
        }