如何用重叠曲线填充 GraphicsPath?
使用.NET的System.Drawing.Graphics
GDI东西,我有一个由两个点数组组成的形状。它们是下图中的红色和绿色像素。
现在我试图用颜色填充这个形状的内部。把它画成简单的线条就可以了。像这样:
g.DrawCurve(Pens.Red, points1);
g.DrawCurve(Pens.Green, points2);
这给出了左图(1)。
替代文本 http://lisa.xms.se/wic/filled.png
至填充这个东西,我尝试使用像这样的GraphicsPath
:
GraphicsPath gp = new GraphicsPath();
gp.AddCurve(points1);
gp.AddCurve(points2);
g.FillPath(Brushes.Blue, gp);
它有效......有点。问题是当形状重叠时,如中间图像 (2) 所示,并且不会填充重叠部分。
我尝试使用 gp.widen() 获取轮廓,然后填充:
gp.Widen(new Pen(Color.Blue, 3));
g.FillPath(Brushes.Blue, gp);
这应该可以,但它似乎只填充了 3 像素切片外部形状不是整个事物,如图 (3) 所示。
有什么想法如何解决这个问题吗?
Using .NET's System.Drawing.Graphics
GDI stuff, I have a shape consting of two arrays of points. They are the red and green pixels in the image below.
Now I am trying to fill the interior of this shape with a color. Drawing it as simple lines works just fine. Like this:
g.DrawCurve(Pens.Red, points1);
g.DrawCurve(Pens.Green, points2);
That gives the left image (1).
alt text http://lisa.xms.se/wic/filled.png
To fill this thing, I tried using a GraphicsPath
like this:
GraphicsPath gp = new GraphicsPath();
gp.AddCurve(points1);
gp.AddCurve(points2);
g.FillPath(Brushes.Blue, gp);
It works... sorta. Problem is when the shape overlap itself as you can see in the middle image (2) and wont fill the overlapping part.
I tried using gp.widen()
to get the outline and then fill after that:
gp.Widen(new Pen(Color.Blue, 3));
g.FillPath(Brushes.Blue, gp);
That ought to work, but it only seems to fill the 3-pixel slice outside the shape not the whole thing, as seen in the image (3).
Any ideas how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,GraphicsPath 使用
FillMode.Alternate
枚举。您需要
FillMode.Winding
缠绕模式会考虑每个交叉点处路径段的方向。它为每个顺时针交叉点加一,并为每个逆时针交叉点减一。如果结果非零,则该点被视为位于填充或剪切区域内。零计数意味着该点位于填充或剪切区域之外。
更多信息请此处
By default the GraphicsPath uses the
FillMode.Alternate
enumeration.You need
FillMode.Winding
The Winding mode considers the direction of the path segments at each intersection. It adds one for every clockwise intersection, and subtracts one for every counterclockwise intersection. If the result is nonzero, the point is considered inside the fill or clip area. A zero count means that the point lies outside the fill or clip area.
More info here