在窗口上绘制位图时不需要的抗锯齿

时间:2011-04-09 16:37:02

标签: c# .net windows graphics gdi+

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

更多细节。像这样创建位图:

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

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

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

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

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

1 个答案:

答案 0 :(得分:9)

将InterpolationMode属性设置为NearestNeighbor,将PixelOffsetMode设置为None。

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

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

相关问题