【问题标题】:Undesired anti-aliasing when drawing bitmap on a window在窗口上绘制位图时不需要的抗锯齿
【发布时间】:2011-08-02 03:41:37
【问题描述】:

我将图像渲染到 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
                )

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

【问题讨论】:

    标签: c# .net windows graphics gdi+


    【解决方案1】:

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

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

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

    【讨论】:

    • 我很确定大多数人都想要一半 PixelOffsetMode
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多