使用 PathGradientBrush 创建的阴影显示不需要的刺结果

A shadow created with PathGradientBrush shows undesired thorn results

我不明白为什么阴影路径在箭头帽附近有荆棘
参考我的截图。

using (GraphicsPath _Path = new GraphicsPath())
{
    Point[] _Points = new Point[] { 
            new Point { X = mouseDownX, Y = mouseDownY }, 
            new Point { X = lineEndX - 51, Y = mouseDownY },
            new Point { X = lineEndX - 51, Y = mouseDownY  - 20 },
            new Point { X = lineEndX, Y = mouseDownY + 5},
            new Point { X = lineEndX -51, Y = mouseDownY + 25},
            new Point { X = lineEndX -51, Y = mouseDownY +10 },
            new Point { X = mouseDownX, Y = mouseDownY +10 }};    
    _Path.AddPolygon(_Points);                                                                                     
    using (PathGradientBrush _Brush = new PathGradientBrush(_Path))
    {
        _Brush.WrapMode = WrapMode.Clamp;
        ColorBlend _ColorBlend = new ColorBlend(3);
        _ColorBlend.Colors = new Color[]{Color.Transparent, 
                                 Color.FromArgb(180, Color.DimGray),
                                 Color.FromArgb(180, Color.DimGray)};
        _ColorBlend.Positions = new float[] { 0f, 0.1f, 1f};
        _Brush.InterpolationColors = _ColorBlend;
        //myGraphics.Clip = new Region(_Path);
        myGraphics.FillPath(_Brush,_Path);
        //myGraphics.ResetClip();
    }
    Matrix _Matrix = new Matrix();
    int _ShadowDistance = -40;
    _Matrix.Translate(_ShadowDistance, _ShadowDistance);
    _Path.Transform(_Matrix);
    myGraphics.FillPath(Brushes.Red, _Path);
}

截图:

纠正该问题的一种方法是指定内部 PathGradientBrushFocusScales,以 delimit 颜色 fall-off,不影响颜色混合。

不幸的是,文档实际上并没有描述这个 属性 的用途。
您可以在此处阅读更好的描述:How to: Create a Path Gradient

fall-off可以调整颜色位置。由于您指定:

Positions = new float[] { 0.0f, 0.1f, 1.0f }

那么颜色fall-off可以设置为:

brush.FocusScales = new PointF(0.1f, 1.0f);

水平比例可以调整,但在总尺寸的一半以内,否则颜色混合会受到影响:您将看不到形状边缘的透明度anti-aliasing。

PixelOffsetMode设置为PixelOffsetMode.Half.
也可以获得更好的结果 注意文档中的描述是错误的,参考C++ documentation about this setting.

示例实现。您可能只想在 MouseDown 事件中刷新绘图。
mousePosition 是鼠标指针的位置。可以在MouseDown事件处理程序中设置。

private Point mousePosition = Point.Empty;
private float lineSize = 100.0f;
private int shadowDistance = 16;

private void someControl_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;

    using (var path = new GraphicsPath(FillMode.Winding)) {
        PointF[] arrowPoints = new PointF[] {
            mousePosition,
            new PointF (mousePosition.X - 20f, mousePosition.Y + 10f),
            new PointF (mousePosition.X - 20f, mousePosition.Y + 3f),
            new PointF (mousePosition.X - lineSize, mousePosition.Y + 3f),
            new PointF (mousePosition.X - lineSize, mousePosition.Y - 3f),
            new PointF (mousePosition.X - 20f, mousePosition.Y - 3f),
            new PointF (mousePosition.X - 20f, mousePosition.Y - 10f)
        };
        path.AddLines(arrowPoints);

        using (var brush = new PathGradientBrush(path.PathPoints, WrapMode.Clamp)) {
            var blend = new ColorBlend() {
                Colors = new Color[] { Color.Transparent,
                                       Color.FromArgb(180, Color.DimGray),
                                       Color.FromArgb(180, Color.DimGray) },
                Positions = new float[] { 0.0f, 0.2f, 1.0f }
            };
            brush.FocusScales = new PointF(0.2f, 1.0f);
            brush.InterpolationColors = blend;
            e.Graphics.FillPath(brush, path);
        }
        using (var mx = new Matrix()) {
            mx.Translate(-shadowDistance, -shadowDistance);
            e.Graphics.Transform = mx;
            e.Graphics.FillPath(Brushes.Red, path);
        }
    }
}