在窗口上绘制位图时不需要的抗锯齿 [英] Undesired anti-aliasing when drawing bitmap on a window

查看:23
本文介绍了在窗口上绘制位图时不需要的抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像渲染到 System.Drawing.Bitmap 中,然后将其绘制到窗口中,但是我可以看到边缘正在消除锯齿.如何防止这种情况?

I'm rendering an image into a System.Drawing.Bitmap and subsequently drawing it into a window, however I can see that the edges are being anti-aliased. How do prevent this?

一些更详细的信息.位图是这样创建的:

Some more detail. The bitmap is created like thus:

new Bitmap (this.Width, this.Height, Imaging.PixelFormat.Format32bppArgb)

然后我将像素设置为 Color.Black 或 Color.White.我尝试使用 Bitmap.SetPixel 和使用 Bitmap.LockBits 将字节直接写入位图数据.

I then set pixels to either Color.Black or Color.White. I've tried using both Bitmap.SetPixel and writing bytes directly to the bitmap data using Bitmap.LockBits.

一旦位图准备好,我就在我的 Form.OnPaint 覆盖中绘制它:

Once the bitmap is ready I draw it in my Form.OnPaint override:

            pea.Graphics.DrawImage
                ( !this.bitmap
                , this.ClientRectangle
                , new Rectangle (0, 0, this.Width, this.Height)
                , GraphicsUnit.Pixel
                )

每个像素都应该是黑色或白色,但我可以看到边缘的像素是灰色的.

Every pixel should either black or white however I can see that pixels at the edges are grey.

推荐答案

将 InterpolationMode 属性设置为 NearestNeighbor 并将 PixelOffsetMode 设置为 None.

Set the InterpolationMode property to NearestNeighbor and PixelOffsetMode to None.

pea.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
pea.Graphics.PixelOffsetMode = PixelOffsetMode.None; // or PixelOffsetMode.Half

绘制未缩放的位图是最好的.在这种情况下,您可能希望使用 ClientSize.Width 和 Height 属性来初始化位图.您现在通过包含表单的边框和标题使位图太大的可能性很大.我无法从片段中看出.

Drawing the bitmap unscaled is best. In which case you probably want to use the ClientSize.Width and Height properties to initialize the bitmap. Odds are good that you are making the bitmap too large right now by including the form's border and caption. I can't tell from the snippet.

这篇关于在窗口上绘制位图时不需要的抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆