gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:54:31 25 4
gpt4 key购买 nike

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

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 shadow

最佳答案

纠正该问题的一种方法是指定内部 PathGradientBrushFocusScales , 在不影响颜色混合的情况下界定颜色衰减。

不幸的是,文档实际上并没有描述这个属性的用途。
您可以在此处阅读更好的描述: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
注意文档中的描述是错误的,引用C++ documentation about this setting .

PathGradientBrush - FocusScales

示例实现。您可能只想在 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);
}
}
}

关于c# - 使用 PathGradientBrush 创建的阴影显示不需要的刺结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56217990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com