如何绘制子像素线

在下面的代码中,我试图绘制两条线:一条具有子像素宽度(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(); 

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

在此处输入图像描述

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

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

根据Pen的文档 ,

Width属性设置为width参数中指定的值。 宽度为0将导致Pen绘图,就好像宽度为1。

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

您可以通过绘制所有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); 

在此处输入图像描述

在这种情况下,我不认为在这里使用子像素是有意义的。 无论如何,您将它导出到图像文件。 请尝试更大的宽度值。

本文提到了GDI + Pen中的一个错误,当它的宽度小于1.5时,它无法正确缩小。

另外,如果你试图分别绘制宽度2.0F 2.5F2.5F3.0F三条线,你会看到一个视觉差异,所以你的情况看起来真的像GDI +的一些问题。

三支笔,0.5宽度增量