我可以用 Pen.DashPattern 做更多事情吗?

Can I do something more with Pen.DashPattern?

因此,我从仅使用颜色作为参数创建 Pen 开始。
除了分配 float[]?

,我还能用 DashPattern 做什么

我尝试使用比较运算符检查它是否为 null。
结果:OutOfMemoryException.

检查可能存在的分配的长度也失败了 - 同样的异常。

问题开始于尝试使用 Buffer.BlockCopy().

将数据从一行 float[,] 传输到 Pen 的 DashPattern

此行为是设计使然。

如果您没有指定 DashStyle different from the default DashStyle.SolidColor or set the Pen.DashPattern to a float[] value, setting, as a consequence, the Pen.DashStyleDashStyle.Custom,则不会设置 Pen.DashPattern 并且本机 GdipGetPenDashCount 方法将 return 一个 Status != GDIP.Ok = 0.

当未设置破折号计数时,默认行为是引发 GDI+ 异常,在本例中为 OutOfMemoryException

你可以做的是测试 Pen DashStyle 并尝试仅在 DashStyle != DashStyle.Solid 时获取它的 DashPattern(明确设置此样式不会创建 DashPattern):

if (pen.DashStyle != DashStyle.Solid)
{
    Console.WriteLine(pen.DashStyle);
    pen.DashPattern.ToList().ForEach(f => Console.WriteLine(f));
}