How to resize an Image C#

前端 未结 17 2642
暗喜
暗喜 2020-11-21 22:28

As Size, Width and Height are Get() properties of System.Drawing.Image;
How can I resize an Image object

17条回答
  •  轮回少年
    2020-11-21 23:10

    This will perform a high quality resize:

    /// 
    /// Resize the image to the specified width and height.
    /// 
    /// The image to resize.
    /// The width to resize to.
    /// The height to resize to.
    /// The resized image.
    public static Bitmap ResizeImage(Image image, int width, int height)
    {
        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);
            }
        }
    
        return destImage;
    }
    
    • wrapMode.SetWrapMode(WrapMode.TileFlipXY) prevents ghosting around the image borders -- naïve resizing will sample transparent pixels beyond the image boundaries, but by mirroring the image we can get a better sample (this setting is very noticeable)
    • destImage.SetResolution maintains DPI regardless of physical size -- may increase quality when reducing image dimensions or when printing
    • Compositing controls how pixels are blended with the background -- might not be needed since we're only drawing one thing.
      • graphics.CompositingMode determines whether pixels from a source image overwrite or are combined with background pixels. SourceCopy specifies that when a color is rendered, it overwrites the background color.
      • graphics.CompositingQuality determines the rendering quality level of layered images.
    • graphics.InterpolationMode determines how intermediate values between two endpoints are calculated
    • graphics.SmoothingMode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) -- probably only works on vectors
    • graphics.PixelOffsetMode affects rendering quality when drawing the new image

    Maintaining aspect ratio is left as an exercise for the reader (actually, I just don't think it's this function's job to do that for you).

    Also, this is a good article describing some of the pitfalls with image resizing. The above function will cover most of them, but you still have to worry about saving.

提交回复
热议问题