在 C# 中使用 DashPattern 画线结果是一条实线

问题描述 投票:0回答:0

我用 [2/3f, 1/3f] 虚线图案绘制一条线,但结果是一条实线。

我还尝试了其他破折号模式,结果如下:

[2/3f, 1/3f]: a solid line
[0.5f, 0.25f]: a good dash line
[0.6f, 0.3f]: a good dash line
[0.66f, 0.33f]: a bad dash line
[2, 1]: a good dash line
using (Bitmap b = new Bitmap(600, 600))
{
    using (Graphics g = Graphics.FromImage(b))
    {
        g.Clear(Color.White);
        
        Pen pen = new Pen(Color.Red, 1);
        pen.DashPattern = new float[] { 2/3f, 1/3f};

        g.DrawLine(pen, 0, 300, 600, 300);
    }
    b.Save("test.png", ImageFormat.Png);
}

c# gdi+
© www.soinside.com 2019 - 2024. All rights reserved.