Richard Knop
Richard Knop

Reputation: 83697

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.

Upvotes: 4

Views: 3930

Answers (3)

Michael
Michael

Reputation: 9058

Instead of doing

g.SmoothingMode = SmoothingMode.None;

you should do

g.InterpolationMode = InterpolationMode.NearestNeighbor;

Upvotes: 11

Gareth Davidson
Gareth Davidson

Reputation: 4917

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

Upvotes: 4

Dave Markle
Dave Markle

Reputation: 97681

Take a look at the InterpolationMode property.

I think that's what you want. Hanselman has a good blog article on it.

Upvotes: 1

Related Questions