How to draw a subpixel line

前端 未结 5 2639
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 22:27

In the following code, I\'m trying to draw two lines: One with a subpixel width (0.5) and the other with 1px width:

        var img = new Bitmap(256, 256);
              


        
5条回答
  •  一生所求
    2021-02-19 23:12

    You could hack it by drawing everything x2 and then scale it down:

            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);
    

    enter image description here

提交回复
热议问题