2016-09-26 172 views
0

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

多边形将是一个混合面膜,我没有问题提请polgons黑色和白色。但是我希望它们能在两种颜色之间逐渐过渡到一定数量的像素,比如50像素(应该指定这个尺寸)。

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

什么得出这样的多边形的最好方法?

+1

您是否实际上使用Microsoft Blend按照您添加的标记描述? – Sayse

+0

你可以发布一个示例图像来清除事情吗? – TaW

回答

1

正如你可以在对方的回答看,渐变画笔用居中的渐变填充路径或多边形;你可以设置中心点,但他们还没有真正遵循多边形边缘:

enter image description here

您可以通过创建一个ColorBlendTransparent,适合Positions,像我一样相互影响色带的相对宽度对于上述结果,但egdes朝向中心点的角度以及它们与边界矩形的距离仍将决定它们的绝对宽度。对于muliticolor渐变画笔例如see here!


所以,除非您的多边形是近圆形,你需要以不同的方式去做。

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

enter image description 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); 
    } 

请注意,左边看起来有点厚,但它确实不是;只是一个错觉..

+0

谢谢,这种在形状边缘绘制不同线条的方法可以用于我想要做的事情。我会试试看。 –

0

我看了一下MSDN上的路径渐变画笔并发现了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); 

和输出的例子:

Example of FocusScales