具有融合边缘的多边形 [英] polygon with blended edges

查看:296
本文介绍了具有融合边缘的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to find the best way to draw polygons in C# with edges that gradually blend into the background color. I'm drawing the polygons to a bitmap, so currently I'm using the Graphics classes from System.Drawing.

多边形将是一个混合蒙版,我可以用黑色和白色绘制多边形.但是我希望它们在一定数量的像素(例如50像素(应指定大小))上逐渐在两种颜色之间过渡.

The polygons will be a blend mask, I have no problem to draw the polgons black and white. But I want them to gradually transition between the two colors over a certain amount of pixels, let's say 50 pixels (that size should be specified).

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

I came across the PathGradiantBrush but I couldn't find a way to specify the size of the transition zone. With that brush the transition seems to depend on the size and shape of the polygon and not be fixed size.

绘制这种多边形的最佳方法是什么?

What's the best way to draw such polygons?

推荐答案

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

As you can see in the other answer, gradient brushes do fill a path or polygon with a centered gradient; you can set the center point but they will still not really follow the polygon's edges:

您可以通过创建一个ColorBlendTransparent并选择一个合适的Positions来影响每个色带的相对宽度,就像我对上述结果所做的那样,但是对象的中心点和它们的中心点之间的角度与边界矩形的距离仍将确定其绝对宽度.有关多色渐变画笔示例,请请参见此处!

You can influence the relative width of each color band by creating a ColorBlend going to Transparent and suitable Positions, as I did for the above result, but the angle of the egdes towards the center point and their distance from the bounding rectangle will still determine their absolute widths. For a muliticolor gradient brush example see here!

因此,除非您的多边形几乎是圆形的,否则您需要进行其他操作.

So, unless your polygon is nearly circular you need to do it differently.

这是一个解决方案,将遵循以下规则:

Here is a solution, which will follow the edges:

使用GraphicsPath path(或简称为Point数组)和Graphics对象g,它首先填充背景色,然后使用宽度和透明度都增加的笔绘制路径.为了使外部边缘既固定又不透明,请设置Pen.Alignment = PenAlignment.Inset.您当然可以玩数字游戏..:

Using a GraphicsPath path (or simply a Point array) and a Graphics object g it first fills in the background color and then draws the path with pens that both grow in width and in transparency. To keep the outer edges both in place and opaque I set the Pen.Alignment = PenAlignment.Inset. You can play with the numbers, of course..:

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);
    }

请注意,左边缘看起来更粗一些,但实际上并非如此.只是一种错觉.

Note that the left edge looks a little thicker but it really isn't; just an optical illusion..

这篇关于具有融合边缘的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆