How to resize an Image C#

前端 未结 17 2393
暗喜
暗喜 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:03

    Resize and save an image to fit under width and height like a canvas keeping image proportional

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace Infra.Files
    {
        public static class GenerateThumb
        {
            /// <summary>
            /// Resize and save an image to fit under width and height like a canvas keeping things proportional
            /// </summary>
            /// <param name="originalImagePath"></param>
            /// <param name="thumbImagePath"></param>
            /// <param name="newWidth"></param>
            /// <param name="newHeight"></param>
            public static void GenerateThumbImage(string originalImagePath, string thumbImagePath, int newWidth, int newHeight)
            {
                Bitmap srcBmp = new Bitmap(originalImagePath);
                float ratio = 1;
                float minSize = Math.Min(newHeight, newHeight);
    
                if (srcBmp.Width > srcBmp.Height)
                {
                    ratio = minSize / (float)srcBmp.Width;
                }
                else
                {
                    ratio = minSize / (float)srcBmp.Height;
                }
    
                SizeF newSize = new SizeF(srcBmp.Width * ratio, srcBmp.Height * ratio);
                Bitmap target = new Bitmap((int)newSize.Width, (int)newSize.Height);
    
                using (Graphics graphics = Graphics.FromImage(target))
                {
                    graphics.CompositingQuality = CompositingQuality.HighSpeed;
                    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphics.CompositingMode = CompositingMode.SourceCopy;
                    graphics.DrawImage(srcBmp, 0, 0, newSize.Width, newSize.Height);
    
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        target.Save(thumbImagePath);
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:05
    public static Image resizeImage(Image image, int new_height, int new_width)
    {
        Bitmap new_image = new Bitmap(new_width, new_height);
        Graphics g = Graphics.FromImage((Image)new_image );
        g.InterpolationMode = InterpolationMode.High;
        g.DrawImage(image, 0, 0, new_width, new_height);
        return new_image;
    }
    
    0 讨论(0)
  • 2020-11-21 23:07

    If you're working with a BitmapSource:

    var resizedBitmap = new TransformedBitmap(
        bitmapSource,
        new ScaleTransform(scaleX, scaleY));
    

    If you want finer control over quality, run this first:

    RenderOptions.SetBitmapScalingMode(
        bitmapSource,
        BitmapScalingMode.HighQuality);
    

    (Default is BitmapScalingMode.Linear which is equivalent to BitmapScalingMode.LowQuality.)

    0 讨论(0)
  • 2020-11-21 23:10

    This will perform a high quality resize:

    /// <summary>
    /// Resize the image to the specified width and height.
    /// </summary>
    /// <param name="image">The image to resize.</param>
    /// <param name="width">The width to resize to.</param>
    /// <param name="height">The height to resize to.</param>
    /// <returns>The resized image.</returns>
    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.

    0 讨论(0)
  • 2020-11-21 23:12
    public string CreateThumbnail(int maxWidth, int maxHeight, string path)
    {
    
        var image = System.Drawing.Image.FromFile(path);
        var ratioX = (double)maxWidth / image.Width;
        var ratioY = (double)maxHeight / image.Height;
        var ratio = Math.Min(ratioX, ratioY);
        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);
        var newImage = new Bitmap(newWidth, newHeight);
        Graphics thumbGraph = Graphics.FromImage(newImage);
    
        thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
        thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
        //thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
        thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
        image.Dispose();
    
        string fileRelativePath = "newsizeimages/" + maxWidth + Path.GetFileName(path);
        newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
        return fileRelativePath;
    }
    

    Click here http://bhupendrasinghsaini.blogspot.in/2014/07/resize-image-in-c.html

    0 讨论(0)
  • 2020-11-21 23:13

    I use ImageProcessorCore, mostly because it works .Net Core.

    And it have more option such as converting types, cropping images and more

    http://imageprocessor.org/imageprocessor/

    0 讨论(0)
提交回复
热议问题