【问题标题】:polygon with blended edges [closed]具有混合边缘的多边形[关闭]
【发布时间】:2016-09-26 19:36:48
【问题描述】:

我试图找到在 C# 中绘制多边形的最佳方法,其边缘逐渐融入背景颜色。我正在将多边形绘制为位图,所以目前我正在使用 System.Drawing 中的 Graphics 类。

多边形将是一个混合蒙版,我可以毫无问题地绘制黑白的多边形。但我希望它们在一定数量的像素上逐渐在两种颜色之间过渡,比如说 50 像素(应该指定该大小)。

我遇到了 PathGradiantBrush,但找不到指定过渡区域大小的方法。使用那个画笔,过渡似乎取决于多边形的大小和形状,而不是固定大小。

绘制此类多边形的最佳方法是什么?

【问题讨论】:

  • 您是否真的按照您添加的标签描述使用 Microsoft Blend?
  • 你能发布一个示例图片来说明问题吗?

标签: c# polygon gradient system.drawing


【解决方案1】:

正如您在另一个答案中看到的那样,渐变画笔确实使用 居中 渐变填充路径或多边形;您可以设置中心点,但它们仍然不会真正跟随多边形的边缘:

您可以通过创建一个ColorBlendTransparent 和合适的Positions 来影响每个色带的相对宽度,就像我对上述结果所做的那样,但是egdes 朝向中心点的角度及其与边界矩形的距离仍将确定它们的绝对宽度。对于多色渐变画笔示例see here!


因此,除非您的多边形几乎是圆形,否则您需要以不同的方式进行操作。

这是一个解决方案,它将遵循边缘:

使用GraphicsPath path(或简单的Point数组)和Graphics对象g,它首先填充背景颜色,然后用宽度和透明度都增加的笔绘制路径。为了使外边缘保持原位且不透明,我设置了Pen.Alignment = PenAlignment.Inset。当然,您可以玩数字游戏..:

g.FillPath(Brushes.MediumSeaGreen, path);

int ew = 8; // edge width

for (int i = 0; i < ew ; i++)
    using (Pen pen = new Pen(Color.FromArgb(255 - i * 255 / ew, Color.DarkSlateBlue), i ))
    {
        pen.Alignment = PenAlignment.Inset;
        g.DrawPath(pen, path);
    }

请注意,左边缘看起来有点厚,但实际上并非如此;只是一种错觉..

【讨论】:

  • 谢谢,这种在形状边缘绘制不同线条的方法可以满足我的需求。我要试一试。
【解决方案2】:

我查看了 MSDN 上的 Path Gradient Brush 并发现了 the FocusScales property,我相信它可以解决您面临的问题。

这是来自this page 的示例,展示了 FocusScales 的使用:

// Create a path that consists of a single ellipse.
GraphicsPath path;
path.AddEllipse(0, 0, 200, 100);

// Create a path gradient brush based on the elliptical path.
PathGradientBrush pthGrBrush(&path);
pthGrBrush.SetGammaCorrection(TRUE);

// Set the color along the entire boundary to blue.
Color color(Color(255, 0, 0, 255));
INT num = 1;
pthGrBrush.SetSurroundColors(&color, &num);

// Set the center color to aqua.
pthGrBrush.SetCenterColor(Color(255, 0, 255, 255));

// Use the path gradient brush to fill the ellipse. 
graphics.FillPath(&pthGrBrush, &path);

// Set the focus scales for the path gradient brush.
pthGrBrush.SetFocusScales(0.3f, 0.8f);

// Use the path gradient brush to fill the ellipse again.
// Show this filled ellipse to the right of the first filled ellipse.
graphics.TranslateTransform(220.0f, 0.0f);
graphics.FillPath(&pthGrBrush, &path);

还有一个输出示例:

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多