Resize bitmap image

48,468

Solution 1

Does your "surface" visual have scaling capability? You can wrap it in a Viewbox if not, then render the Viewbox at the size you want.

When you call Measure and Arrange on the surface, you should provide the size you want the bitmap to be.

To use the Viewbox, change your code to something like the following:

Viewbox viewbox = new Viewbox();
Size desiredSize = new Size(surface.Width / 2, surface.Height / 2);

viewbox.Child = surface;
viewbox.Measure(desiredSize);
viewbox.Arrange(new Rect(desiredSize));

RenderTargetBitmap renderBitmap =
    new RenderTargetBitmap(
    (int)desiredSize.Width,
    (int)desiredSize.Height, 96d, 96d,
    PixelFormats.Default);
renderBitmap.Render(viewbox);

Solution 2

public static Bitmap ResizeImage(Bitmap imgToResize, Size size)
{
    try
    {
        Bitmap b = new Bitmap(size.Width, size.Height);
        using (Graphics g = Graphics.FromImage((Image)b))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
        }
        return b;
    }
    catch 
    { 
        Console.WriteLine("Bitmap could not be resized");
        return imgToResize; 
    }
}

Solution 3

The shortest way to resize a Bitmap is to pass it to a Bitmap-constructor together with the desired size (or width and height):

bitmap = new Bitmap(bitmap, width, height);
Share:
48,468
Ionică Bizău
Author by

Ionică Bizău

💻 Programmer ⚡️ Geek 🎹 Pianist & Organist 🚀 Learner 💡 Mentor 💫 Dreamer 🍏 Vegetarian 🙏 Jesus follower Website | GitHub | Twitter | PayPal Donations

Updated on December 22, 2020

Comments

  • Ionică Bizău
    Ionică Bizău over 3 years

    I want to have smaller size at image saved. How can I resize it? I use this code for redering the image:

    Size size = new Size(surface.Width, surface.Height);
    surface.Measure(size);
    surface.Arrange(new Rect(size));
    // Create a render bitmap and push the surface to it
    RenderTargetBitmap renderBitmap =
        new RenderTargetBitmap(
            (int)size.Width,
            (int)size.Height, 96d, 96d,
            PixelFormats.Default);
    renderBitmap.Render(surface);
    
    BmpBitmapEncoder encoder = new BmpBitmapEncoder();
    // push the rendered bitmap to it
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
    // save the data to the stream
    encoder.Save(outStream);
    
  • Chris Shouts
    Chris Shouts about 9 years
    This is perfect without the try-catch block.
  • Max von Hippel
    Max von Hippel almost 9 years
    The Size part is important, I found multiple old answers using 2 ints but you need a size now. Thanks for noticing that, saved some trouble. (Commenting for sake of future viewers to know answers using 2 ints need to be changed accordingly)
  • srking
    srking over 7 years
    Worked for me. Have an upvote to compensate for somebody's bad manners.
  • theMohammedA
    theMohammedA over 7 years
    Worked perfectly.
  • Russell Bearden
    Russell Bearden over 6 years
    A downside with this solution is that the default scaling is optimized for speed in exchange for quality. Depending on what (and to what size) is being scaled, this will produce aliasing and other graphical artifacts. The solution by Kashif enables selecting the scaling interpolation algorithm.