Scaling a System.Drawing.Bitmap to a given size while maintaining aspect ratio

前端 未结 3 613
慢半拍i
慢半拍i 2020-11-28 08:35

I want to scale a System.Drawing.Bitmap to at least less than some fixed width and height. This is to generate thumbnails for an image gallery on a website, so

相关标签:
3条回答
  • 2020-11-28 09:07

    Just to add to yamen's answer, which is perfect for images but not so much for text.

    If you are trying to use this to scale text, like say a Word document (which is in this case in bytes from Word Interop), you will need to make a few modifications or you will get giant bars on the side.

    May not be perfect but works for me!

    using (MemoryStream ms = new MemoryStream(wordBytes))
    {
        float width = 3840;
        float height = 2160;
        var brush = new SolidBrush(Color.White);
    
        var rawImage = Image.FromStream(ms);
        float scale = Math.Min(width / rawImage.Width, height / rawImage.Height);
        var scaleWidth  = (int)(rawImage.Width  * scale);
        var scaleHeight = (int)(rawImage.Height * scale);
        var scaledBitmap = new Bitmap(scaleWidth, scaleHeight);
    
        Graphics graph = Graphics.FromImage(scaledBitmap);
        graph.InterpolationMode = InterpolationMode.High;
        graph.CompositingQuality = CompositingQuality.HighQuality;
        graph.SmoothingMode = SmoothingMode.AntiAlias;
        graph.FillRectangle(brush, new RectangleF(0, 0, width, height));
        graph.DrawImage(rawImage, new Rectangle(0, 0 , scaleWidth, scaleHeight));
    
        scaledBitmap.Save(fileName, ImageFormat.Png);
        return scaledBitmap;
    }
    
    0 讨论(0)
  • 2020-11-28 09:12

    The bitmap constructor has resizing built in.

    Bitmap original = (Bitmap)Image.FromFile("DSC_0002.jpg");
    Bitmap resized = new Bitmap(original,new Size(original.Width/4,original.Height/4));
    resized.Save("DSC_0002_thumb.jpg");
    

    http://msdn.microsoft.com/en-us/library/0wh0045z.aspx

    If you want control over interpolation modes see this post.

    0 讨论(0)
  • 2020-11-28 09:14

    Target parameters:

    float width = 1024;
    float height = 768;
    var brush = new SolidBrush(Color.Black);
    

    Your original file:

    var image = new Bitmap(file);
    

    Target sizing (scale factor):

    float scale = Math.Min(width / image.Width, height / image.Height);
    

    The resize including brushing canvas first:

    var bmp = new Bitmap((int)width, (int)height);
    var graph = Graphics.FromImage(bmp);
    
    // uncomment for higher quality output
    //graph.InterpolationMode = InterpolationMode.High;
    //graph.CompositingQuality = CompositingQuality.HighQuality;
    //graph.SmoothingMode = SmoothingMode.AntiAlias;
    
    var scaleWidth = (int)(image.Width * scale);
    var scaleHeight = (int)(image.Height * scale);
    
    graph.FillRectangle(brush, new RectangleF(0, 0, width, height));
    graph.DrawImage(image, ((int)width - scaleWidth)/2, ((int)height - scaleHeight)/2, scaleWidth, scaleHeight);
    

    And don't forget to do a bmp.Save(filename) to save the resulting file.

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