Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a bitmap image in C# without blending or filtering?

I have a gray scale image that I would like to enlarge so that I can better see the individual pixels. I've tried setting Smoothing mode to none and some different Interpolation Modes (as suggested on other questions on here), but the images still appear to me as if they are still doing some sort of blending before being displayed on the screen.

basically If I have a image that is

(White, White,
 White, Black)

I want when I enlarge it to say 6x6, it to look like

 (White, White, White, White, White, White
  White, White, White, White, White, White
  White, White, White, White, White, White
  White, White, White, Black, Black, Black
  White, White, White, Black, Black, Black
  White, White, White, Black, Black, Black)

With no fading between the black and white areas, should look like a square. The image should look more "pixelized" rather then "Blurry"

like image 309
hrh Avatar asked Jul 12 '12 16:07

hrh


2 Answers

Try to set interpolation mode:

g.InterpolationMode = InterpolationMode.NearestNeighbor;
like image 169
Raman Zhylich Avatar answered Oct 04 '22 05:10

Raman Zhylich


I too was looking to do something similar. When I found this question neither answer were quite what I was looking for but combined they were. This is what got me to where I wanted to be and from what I can tell from your question what you want.

private Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height)
{
    Bitmap result = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage(result))
    {
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
        g.DrawImage(sourceBMP, 0, 0, width, height);
    }
    return result;
}
like image 24
Soltris Avatar answered Oct 04 '22 06:10

Soltris