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