简体   繁体   中英

Resizing a monochromatic image in C#

I have the following code to resize a monochromatic image (hence pixel value is 0[black] or 255[white]) with the following code

        Bitmap ResizedCharImage = new Bitmap(newwidth, newheight);

        using (Graphics g = Graphics.FromImage((Image)ResizedCharImage))
        {
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(CharBitmap, new Rectangle(0, 0, newwidth, newheight),
                new Rectangle(0, 0, CharBitmap.Width, CharBitmap.Height), GraphicsUnit.Pixel);
        }

The problem that I am having is that after resizing (i am enlarging the image) some of the pixel values become 254, 253, 1, 2 etc. (and so are not monochromatic.) I need that this do not occur. Is this possible, maybe by changing one of the Graphins properties?

使用SmoothingMode.None

通过将InterpolationMode设置为显然解决了问题

InterpolationMode.NearestNeighbor;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM