Beispiel #1
0
        public void Resize()
        {
            var file = "test3.jpg";

            MagickWandEnvironment.Genesis();

            using(var mw = new MagickWand())
            {
                mw.ReadImage("test.jpg");
                mw.ResizeImage(120, 100, FilterTypes.LanczosFilter, 1);
                mw.WriteImage(file, true);

                Assert.True(File.Exists(file), "scaled image not created");
            }

            using(var mw = new MagickWand(file))
            {
                Assert.True(mw.ImageWidth == 120, "width does not match the expected size");
                Assert.True(mw.ImageHeight == 100, "height does not match the expected size");
            }

            File.Delete(file);

            MagickWandEnvironment.Terminus();
        }
Beispiel #2
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());
        }
Beispiel #3
0
        public void SaveResizedImage(int newWidth, int newHeight, string outPath)
        {
            CreateDirectoryForFile(outPath);
            using (MagickWand wand = new MagickWand()) {
                wand.ReadImage(filename);

                if (width > newWidth && height > newHeight) {
                    wand.ResizeImage((uint)newWidth, (uint)newHeight);
                } else {
                    // XXX: Make a new image that's newwidth by newheight
                    // and center old image in it
                }

                wand.WriteImage (outPath);
            }
        }
Beispiel #4
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());
        }