Exemplo n.º 1
0
        /// <summary>
        /// Specify custom resize options
        /// </summary>
        /// <param name="img">the image to resize</param>
        /// <param name="source">The coordinates to read as source from the image,
        /// can be the whole image or part of it</param>
        /// <param name="target">The coordinates of the target image size</param>
        /// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
        /// <returns></returns>
        public static Image Resize(this Image img, Rectangle source, Rectangle target, GraphicOptions ops)
        {
            var pixF = ImageColorFormats.GetColorFormat((Bitmap)img) == ImageColorFormat.Cmyk
                ? PixelFormat.Format32bppArgb
                : img.PixelFormat;

            using (Bitmap outputImage = new Bitmap(target.Width, target.Height, pixF))
            {
                outputImage.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                using (var graphics = Graphics.FromImage(outputImage))
                {
                    graphics.SmoothingMode      = ops.SmoothingMode;
                    graphics.InterpolationMode  = ops.InterpolationMode;
                    graphics.PixelOffsetMode    = ops.PixelOffsetMode;
                    graphics.CompositingQuality = ops.CompositingQuality;
                    graphics.CompositingMode    = ops.CompositingMode;
                    graphics.PageUnit           = ops.PageUnit;

                    graphics.DrawImage(
                        img,
                        target,
                        source,
                        ops.PageUnit);
                }

                return(Image.FromHbitmap(outputImage.GetHbitmap()));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Specify custom resize options
        /// </summary>
        /// <param name="img">the image to resize</param>
        /// <param name="source">The coordinates to read as source from the image,
        /// can be the whole image or part of it</param>
        /// <param name="target">The coordinates of the target image size</param>
        /// <param name="ops">Graphic options <see cref="GraphicOptions"/></param>
        /// <returns></returns>
        public static Image Resize(this Image img, Rectangle source, Rectangle target, GraphicOptions ops)
        {
            // check for CMYK pixel format to use Format32bppArgb
            // or use the image pixel format
            var pixF = ImageColorFormats.GetColorFormat((Bitmap)img) == ImageColorFormat.Cmyk
                ? PixelFormat.Format32bppArgb
                : img.PixelFormat;

            using (Bitmap outputImage = new Bitmap(target.Width, target.Height, pixF))
            {
                outputImage.SetResolution(img.HorizontalResolution, img.VerticalResolution);

                using (var graphics = Graphics.FromImage(outputImage))
                {
                    graphics.SmoothingMode      = ops.SmoothingMode;
                    graphics.InterpolationMode  = ops.InterpolationMode;
                    graphics.PixelOffsetMode    = ops.PixelOffsetMode;
                    graphics.CompositingQuality = ops.CompositingQuality;
                    graphics.CompositingMode    = ops.CompositingMode;
                    graphics.PageUnit           = ops.PageUnit;

                    graphics.DrawImage(
                        img,
                        target,
                        source,
                        ops.PageUnit);
                }

                // If the image has alpha channel (png) return image memory stream
                if (img.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    using (var ms = new MemoryStream())
                    {
                        outputImage.Save(ms, img.RawFormat);
                        return(Image.FromStream(ms));
                    }
                }

                return(Image.FromHbitmap(outputImage.GetHbitmap()));
            }
        }