在 C# 中调整单色图像的大小

发布于 2024-08-28 21:31:06 字数 776 浏览 4 评论 0原文

我有以下代码可以使用以下代码调整单色图像的大小(因此像素值为 0[黑色] 或 255[白色])

        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);
        }

我遇到的问题是在调整大小(我放大图像)后某些像素值变成 254、253、1、2 等。(因此不是单色的。)我需要这种情况不会发生。这可能吗,也许可以通过更改 Graphins 属性之一来实现?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无语# 2024-09-04 21:31:07

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

InterpolationMode.NearestNeighbor;

apparently problem solved by setting InterpolationMode to

InterpolationMode.NearestNeighbor;
沉鱼一梦 2024-09-04 21:31:06

使用SmoothingMode.None

Use SmoothingMode.None

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文