我不明白为什么阴影路径在箭帽附近有刺。
请参阅我的屏幕截图。
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);
}
屏幕截图:
最佳答案
解决问题的一种方法是指定内部PathGradientBrush
的FocusScales,以限制颜色下降,而不会影响颜色混合。
不幸的是,文档并未真正描述此属性的用途。
您可以在此处阅读更好的描述:How to: Create a Path Gradient
不幸的是,缺少实现该描述的代码。
可以在颜色位置上调整衰减。由于您指定了:
Positions = new float[] { 0.0f, 0.1f, 1.0f }
颜色衰减可以设置为:
brush.FocusScales = new PointF(0.1f, 1.0f);
可以调整水平比例尺,但不得超过总比例尺的一半,否则会影响颜色混合:您不会在形状的边缘看到透明度抗锯齿。
将PixelOffsetMode设置为
PixelOffsetMode.Half
也可以获得更好的结果。不幸的是,这里的Docs描述是错误的,请参考C++ documentation about this setting。
通用实现(使用Panel作为画布):
(您可能只想在
MouseDown
事件上启用绘画)mousePosition
是鼠标指针的位置。可以在MouseDown
事件上设置。private Point mousePosition = Point.Empty;
private float lineSize = 100.0f;
private int shadowDistance = 16;
private void panel1_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);
}
}
}