Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize a bitmap like MS Paint - no antialiasing

When I use this method to resize a bitmap:

    private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
    {
        Bitmap result = new Bitmap(nWidth, nHeight);
        using (Graphics g = Graphics.FromImage((Image)result))
        {
            g.SmoothingMode = SmoothingMode.None;
            g.DrawImage(b, 0, 0, nWidth, nHeight);
        }
        return result;
    }

It still uses antialiasing even though I specified:

g.SmoothingMode = SmoothingMode.None;

I want just a basic resizing without any smoothing.

like image 679
Richard Knop Avatar asked Dec 07 '22 00:12

Richard Knop


2 Answers

Instead of doing

g.SmoothingMode = SmoothingMode.None;

you should do

g.InterpolationMode = InterpolationMode.NearestNeighbor;
like image 123
Michael Avatar answered Dec 23 '22 16:12

Michael


Anti-aliasing is a sub-pixel thing, you're actually looking for Nearest Neighbour interpolation during the resize operation.

like image 44
Gareth Davidson Avatar answered Dec 23 '22 17:12

Gareth Davidson