如何绘制子像素线

Fre*_*edL 12 .net c# graphics system.drawing

在下面的代码中,我试图绘制两条线:一条具有子像素宽度(0.5),另一条具有1px宽度:

        var img = new Bitmap(256, 256);
        Graphics graphics = Graphics.FromImage(img);
        graphics.SmoothingMode = SmoothingMode.AntiAlias;

        // Draw a subpixel line (0.5 width)
        graphics.DrawLine(new Pen(Color.Red, (float)0.5), 0, 100, 255, 110);

        // Draw a single pixel line (1 width)
        graphics.DrawLine(new Pen(Color.Red, (float)1), 0, 110, 255, 120);

        img.Save(@"c:\temp\test.png", ImageFormat.Png);

        graphics.Dispose();

        img.Dispose();
Run Code Online (Sandbox Code Playgroud)

但是,在生成的图像中,两条线显示相同的宽度:

在此输入图像描述

有没有办法让顶线出现亚像素(0.5px)?

编辑:经过一些研究,AGG可能是要走的路,其中有一个c#端口.

gor*_*rdy 7

您可以通过绘制所有x2然后缩小它来破解它:

        Image img2x = new Bitmap(256*2, 256*2);
        Graphics g2x = Graphics.FromImage(img2x);
        g2x.SmoothingMode = SmoothingMode.AntiAlias;
        g2x.DrawLine(new Pen(Color.Red, 0.5f*2), 0, 100*2, 255*2, 110*2);

        Image img = new Bitmap(256, 256);
        Graphics g = Graphics.FromImage(img);
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.DrawImage(img2x, 0, 0, 256, 256);

        g.DrawLine(new Pen(Color.Red, 1f), 0, 110, 255, 120);

        img.Save(@"c:\tmep\test.png", ImageFormat.Png);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Dat*_*han 2

根据的文档Pen

Width 属性设置为宽度参数中指定的值。宽度为 0 将导致钢笔绘图,就像宽度为 1 一样。

这可能适用于任何小于 1 的宽度,而不仅仅是精确等于 0 的宽度。