Parameters for the ImageResizer.
Exemplo n.º 1
0
        private void Resize(Bitmap original, ImageResizeParameters parameters, Stream output)
        {
            Bitmap resized;
            var    mode      = parameters.Mode;
            var    maxWidth  = parameters.MaxWidth;
            var    maxHeight = parameters.MaxHeight;
            var    quality   = parameters.Quality;

            if (mode == ImageResizeMode.Fit)
            {
                double resizeRation = GetResizeRatio(original, maxWidth, maxHeight);
                int    newWidth     = (int)Math.Round(original.Width * resizeRation);
                int    newHeight    = (int)Math.Round(original.Height * resizeRation);
                resized = new Bitmap(newWidth, newHeight, original.PixelFormat);
            }
            else
            {
                resized = new Bitmap((int)maxWidth, (int)maxHeight, original.PixelFormat);
            }

            resized.SetResolution(original.HorizontalResolution, original.VerticalResolution);

            Graphics g = CreateGraphics(original, ref resized);

            using (g)
            {
                g.PageUnit           = GraphicsUnit.Pixel;
                g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode      = SmoothingMode.HighQuality;
                g.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                g.CompositingMode    = CompositingMode.SourceCopy;
                g.CompositingQuality = CompositingQuality.HighQuality;

                using (ImageAttributes attr = new ImageAttributes())
                {
                    attr.SetWrapMode(WrapMode.TileFlipXY);

                    Rectangle destinationFrame = mode == ImageResizeMode.Fill
                                                ? GetFillDestinationRectangle(original.Size, resized.Size)
                                                : new Rectangle(Point.Empty, resized.Size);
                    g.DrawImage(original, destinationFrame, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attr);
                }

                // Use higher quality compression if the original image is jpg. Default is 75L.
                ImageCodecInfo codec = GetEncoderInfo(original.RawFormat.Guid);

                if (codec != null && codec.MimeType.Equals("image/jpeg"))
                {
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);

                    resized.Save(output, codec, encoderParams);
                }
                else
                {
                    resized.Save(output, original.RawFormat);
                }
            }
        }
Exemplo n.º 2
0
 public virtual bool Resize(Stream inputStream, ImageResizeParameters parameters, Stream outputStream)
 {
     using (Bitmap original = new Bitmap(inputStream))
     {
         Resize(original, parameters, outputStream);
         return(true);
     }
 }
Exemplo n.º 3
0
 public virtual byte[] GetResizedBytes(Stream imageStream, ImageResizeParameters parameters)
 {
     using (Bitmap original = new Bitmap(imageStream))
     {
         var ms = new MemoryStream();
         Resize(original, parameters, ms);
         return(ms.GetBuffer());
     }
 }
Exemplo n.º 4
0
		public virtual bool Resize(Stream inputStream, ImageResizeParameters parameters, Stream outputStream)
		{
			if (inputStream == null) throw new ArgumentNullException("inputStream");

			using (Bitmap original = new Bitmap(inputStream))
			{
				Resize(original, parameters, outputStream);
				return true;
			}
		}
Exemplo n.º 5
0
		public virtual byte[] GetResizedBytes(Stream imageStream, ImageResizeParameters parameters)
		{
			if (imageStream == null) throw new ArgumentNullException("imageStream");

			using (Bitmap original = new Bitmap(imageStream))
			{
				var ms = new MemoryStream();
				Resize(original, parameters, ms);
				return ms.GetBuffer();
			}
		}
Exemplo n.º 6
0
        public virtual bool Resize(Stream inputStream, ImageResizeParameters parameters, Stream outputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }

            using (Bitmap original = new Bitmap(inputStream))
            {
                Resize(original, parameters, outputStream);
                return(true);
            }
        }
Exemplo n.º 7
0
        public virtual byte[] GetResizedBytes(Stream imageStream, ImageResizeParameters parameters)
        {
            if (imageStream == null)
            {
                throw new ArgumentNullException("imageStream");
            }

            using (Bitmap original = new Bitmap(imageStream))
            {
                var ms = new MemoryStream();
                Resize(original, parameters, ms);
                return(ms.GetBuffer());
            }
        }
Exemplo n.º 8
0
        private void Resize(Bitmap original, ImageResizeParameters parameters, Stream output)
        {
            Bitmap resized = null;
            try
            {
                var mode = parameters.Mode;
                var maxWidth = parameters.MaxWidth;
                var maxHeight = parameters.MaxHeight;
                var quality = parameters.Quality;
                var srcRect = parameters.SourceRectangle;
                Rectangle dest;
                double resizeRatio;
                int newWidth, newHeight;

                switch (mode)
                {
                    case ImageResizeMode.Fit:
                        resizeRatio = GetResizeRatio(original, maxWidth, maxHeight);
                        newWidth = (int)Math.Round(original.Width * resizeRatio);
                        newHeight = (int)Math.Round(original.Height * resizeRatio);
                        resized = new Bitmap(newWidth, newHeight, original.PixelFormat);
                        dest = new Rectangle(Point.Empty, resized.Size);
                        break;
                    case ImageResizeMode.FitCenterOnTransparent:
                        resizeRatio = GetResizeRatio(original, maxWidth, maxHeight);
                        newWidth = (int)Math.Round(original.Width * resizeRatio);
                        newHeight = (int)Math.Round(original.Height * resizeRatio);
                        int newImageX = (maxWidth < maxHeight) ? 0 : ((int)((maxWidth - (original.Width * resizeRatio)) / 2));
                        int newImageY = newImageX != 0 ? 0 : ((int)((maxHeight - (original.Height * resizeRatio)) / 2));
                        resized = new Bitmap((int)maxWidth, (int)maxHeight, PixelFormat.Format32bppArgb);
                        dest = new Rectangle(newImageX, newImageY, newWidth, newHeight);
                        break;
                    default:
                        resized = new Bitmap((int)maxWidth, (int)maxHeight, original.PixelFormat);
                        dest = new Rectangle(Point.Empty, resized.Size);
                        break;
                }

                resized.SetResolution(original.HorizontalResolution, original.VerticalResolution);

                using (Graphics g = CreateGraphics(original, ref resized))
                {
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    g.CompositingMode = CompositingMode.SourceCopy;
                    g.CompositingQuality = CompositingQuality.HighQuality;

                    using (ImageAttributes attr = new ImageAttributes())
                    {
                        attr.SetWrapMode(WrapMode.TileFlipXY);
                        var originalSize = srcRect.HasValue ? srcRect.Value.Size : original.Size;

                        if (mode == ImageResizeMode.Fill)
                            dest = GetFillDestinationRectangle(originalSize, resized.Size);

                        var src = new Rectangle(0, 0, original.Width, original.Height);
                        if (srcRect.HasValue)
                            src = srcRect.Value;

                        if (mode == ImageResizeMode.FitCenterOnTransparent)
                            g.Clear(Color.Transparent);

                        g.DrawImage(original, dest, src.X, src.Y, src.Width, src.Height, GraphicsUnit.Pixel, attr);
                    }

                    // Use higher quality compression if the original image is jpg. Default is 75L.
                    var codec = GetEncoderInfo(original.RawFormat.Guid);
                    if (codec != null && codec.MimeType.Equals("image/jpeg"))
                    {
                        EncoderParameters encoderParams = new EncoderParameters(1);
                        encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);
                        resized.Save(output, codec, encoderParams);
                    }
                    else
                    {
                        resized.Save(output, original.RawFormat);
                    }
                }
            }
            finally
            {
                if (resized != null)
                    resized.Dispose();
            }
        }
Exemplo n.º 9
0
        private void Resize(Bitmap original, ImageResizeParameters parameters, Stream output)
        {
            Bitmap resized = null;

            try
            {
                var       mode      = parameters.Mode;
                var       maxWidth  = parameters.MaxWidth;
                var       maxHeight = parameters.MaxHeight;
                var       quality   = parameters.Quality;
                var       srcRect   = parameters.SourceRectangle;
                Rectangle dest;
                double    resizeRatio;
                int       newWidth, newHeight;

                switch (mode)
                {
                case ImageResizeMode.Fit:
                    resizeRatio = GetResizeRatio(original, maxWidth, maxHeight);
                    newWidth    = (int)Math.Round(original.Width * resizeRatio);
                    newHeight   = (int)Math.Round(original.Height * resizeRatio);
                    resized     = new Bitmap(newWidth, newHeight, original.PixelFormat);
                    dest        = new Rectangle(Point.Empty, resized.Size);
                    break;

                case ImageResizeMode.FitCenterOnTransparent:
                    resizeRatio = GetResizeRatio(original, maxWidth, maxHeight);
                    newWidth    = (int)Math.Round(original.Width * resizeRatio);
                    newHeight   = (int)Math.Round(original.Height * resizeRatio);
                    int newImageX = (maxWidth < maxHeight) ? 0 : ((int)((maxWidth - (original.Width * resizeRatio)) / 2));
                    int newImageY = newImageX != 0 ? 0 : ((int)((maxHeight - (original.Height * resizeRatio)) / 2));
                    resized = new Bitmap((int)maxWidth, (int)maxHeight, PixelFormat.Format32bppArgb);
                    dest    = new Rectangle(newImageX, newImageY, newWidth, newHeight);
                    break;

                default:
                    resized = new Bitmap((int)maxWidth, (int)maxHeight, original.PixelFormat);
                    dest    = new Rectangle(Point.Empty, resized.Size);
                    break;
                }


                resized.SetResolution(original.HorizontalResolution, original.VerticalResolution);

                using (Graphics g = CreateGraphics(original, ref resized))
                {
                    g.PageUnit           = GraphicsUnit.Pixel;
                    g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode      = SmoothingMode.HighQuality;
                    g.PixelOffsetMode    = PixelOffsetMode.HighQuality;
                    g.CompositingMode    = CompositingMode.SourceCopy;
                    g.CompositingQuality = CompositingQuality.HighQuality;

                    using (ImageAttributes attr = new ImageAttributes())
                    {
                        attr.SetWrapMode(WrapMode.TileFlipXY);
                        var originalSize = srcRect.HasValue ? srcRect.Value.Size : original.Size;

                        if (mode == ImageResizeMode.Fill)
                        {
                            dest = GetFillDestinationRectangle(originalSize, resized.Size);
                        }

                        var src = new Rectangle(0, 0, original.Width, original.Height);
                        if (srcRect.HasValue)
                        {
                            src = srcRect.Value;
                        }

                        if (mode == ImageResizeMode.FitCenterOnTransparent)
                        {
                            g.Clear(Color.Transparent);
                        }

                        g.DrawImage(original, dest, src.X, src.Y, src.Width, src.Height, GraphicsUnit.Pixel, attr);
                    }

                    // Use higher quality compression if the original image is jpg. Default is 75L.
                    var codec = GetEncoderInfo(original.RawFormat.Guid);
                    if (codec != null && codec.MimeType.Equals("image/jpeg"))
                    {
                        EncoderParameters encoderParams = new EncoderParameters(1);
                        encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);
                        resized.Save(output, codec, encoderParams);
                    }
                    else
                    {
                        resized.Save(output, original.RawFormat);
                    }
                }
            }
            finally
            {
                if (resized != null)
                {
                    resized.Dispose();
                }
            }
        }
Exemplo n.º 10
0
 public virtual byte[] GetResizedBytes(Stream imageStream, ImageResizeParameters parameters)
 {
     using (Bitmap original = new Bitmap(imageStream))
     {
         var ms = new MemoryStream();
         Resize(original, parameters, ms);
         return ms.GetBuffer();
     }
 }
Exemplo n.º 11
0
        private void Resize(Bitmap original, ImageResizeParameters parameters, Stream output)
        {
            Bitmap resized;
            var mode = parameters.Mode;
            var maxWidth = parameters.MaxWidth;
            var maxHeight = parameters.MaxHeight;
            var quality = parameters.Quality;

            if (mode == ImageResizeMode.Fit)
            {
                double resizeRation = GetResizeRatio(original, maxWidth, maxHeight);
                int newWidth = (int)Math.Round(original.Width * resizeRation);
                int newHeight = (int)Math.Round(original.Height * resizeRation);
                resized = new Bitmap(newWidth, newHeight, original.PixelFormat);
            }
            else
                resized = new Bitmap((int)maxWidth, (int)maxHeight, original.PixelFormat);

            resized.SetResolution(original.HorizontalResolution, original.VerticalResolution);

            Graphics g = CreateGraphics(original, ref resized);

            using (g)
            {
                g.PageUnit = GraphicsUnit.Pixel;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.CompositingMode = CompositingMode.SourceCopy;
                g.CompositingQuality = CompositingQuality.HighQuality;

                using (ImageAttributes attr = new ImageAttributes())
                {
                    attr.SetWrapMode(WrapMode.TileFlipXY);

                    Rectangle destinationFrame = mode == ImageResizeMode.Fill
                        ? GetFillDestinationRectangle(original.Size, resized.Size)
                        : new Rectangle(Point.Empty, resized.Size);
                    g.DrawImage(original, destinationFrame, 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attr);
                }

                // Use higher quality compression if the original image is jpg. Default is 75L.
                ImageCodecInfo codec = GetEncoderInfo(original.RawFormat.Guid);

                if (codec != null && codec.MimeType.Equals("image/jpeg"))
                {
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)quality);

                    resized.Save(output, codec, encoderParams);
                }
                else
                {
                    resized.Save(output, original.RawFormat);
                }
            }
        }
Exemplo n.º 12
0
 public virtual bool Resize(Stream inputStream, ImageResizeParameters parameters, Stream outputStream)
 {
     using (Bitmap original = new Bitmap(inputStream))
     {
         Resize(original, parameters, outputStream);
         return true;
     }
 }