DEV Community

Cover image for Resize Bitmap without major quality loss
Adam K Dean
Adam K Dean

Posted on

Resize Bitmap without major quality loss

I'm working on a computer vision project at the moment. Whilst working on it, I've had to resize a few bitmaps due to the webcam library I'm using not being able to properly set the resolution of the camera.

I found this on StackOverflow, and it works quite well. There are a few quality differences, such loss of sharpness due to smoothing, but it's only noticeable when you put the two images side to side.

Bitmap resized = new Bitmap(desiredHeight, desiredWidth);
using (Graphics graphics = Graphics.FromImage(resized))
{
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    graphics.DrawImage(originalImage,
        new Rectangle(0, 0, desiredHeight, desiredWidth));
}
pictureBox1.Image = resized;
Enter fullscreen mode Exit fullscreen mode

Like I said, works quite well, and definitely one for the snippet bin!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay