Example #1
0
        private byte[] ResizeImage(System.Drawing.Image image, int width, int height)
        {
            var stream = new MemoryStream();

            image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            try {
                var wand = new MagickWand();
                wand.ReadImageBlob(stream.ToArray());
                wand.ResizeImage(width, height, FilterType.Mitchell, 0.5);
                return(wand.GetImageBlob());
            } catch (Exception ex) {
                Debug.Fail(ex.ToString());
            }
            return(stream.ToArray());
        }
Example #2
0
        /// <summary>
        /// Uses ImageMagick to convert icon to grayscale and lighten it
        /// </summary>
        private byte[] ApplyDisabledStyling(System.Drawing.Image image)
        {
            var stream = new MemoryStream();

            image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            try {
                var wand = new MagickWand();
                wand.ReadImageBlob(stream.ToArray());
                wand.ImageType = ImageType.GrayscaleMatte;
                wand.EvaluateImage(MagickEvaluateOperator.DivideEvaluateOperator, 4);
                return(wand.GetImageBlob());
            } catch (Exception ex) {
                Debug.Fail(ex.ToString());
            }
            return(stream.ToArray());
        }
Example #3
0
        byte[] ResizeImage(Image image, int width, int height)
        {
            var stream = new MemoryStream();

            image.Save(stream, ImageFormat.Png);
            try {
                var wand = new MagickWand();
                wand.ReadImageBlob(stream.ToArray());
                wand.ResizeImage(width, height, FilterType.Mitchell, 0.5);
                return(wand.GetImageBlob());
            } catch (Exception ex) {
                Debug.Fail(ex.ToString());

                // http://stackoverflow.com/a/24199315/1976323

                var destRect  = new Rectangle(0, 0, width, height);
                var destImage = new Bitmap(width, height);

                destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                using (var graphics = Graphics.FromImage(destImage))
                {
                    graphics.CompositingMode    = CompositingMode.SourceCopy;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode      = SmoothingMode.HighQuality;
                    graphics.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                    using (var wrapMode = new ImageAttributes())
                    {
                        wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                        graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                destImage.Save(stream, ImageFormat.Png);
            }
            return(stream.ToArray());
        }