是否可以用c#中的Graphics Class填写一个字母?

时间:2015-08-27 15:39:12

标签: c# system.drawing drawstring

我正在开发一个项目,可以在c#中在地图上绘制对象。我们使用自己创建的True Typed字体(.ttf)。我们这样做的原因是因为用户必须能够根据需要为对象指定自己的图标。

我正在绘制的字体(字母)是一个轮廓图标记,如图所示

Map markesr

这些数字是稍后绘制的,但正如您所看到的,由于背景原因,这个数字还不够清晰。

我现在想要做的是将标记填充为白色,而不是透明。

我按照以下方式绘制标记:

GraphicsPath p = new GraphicsPath();
p.AddString(MarkerSymbol, markerFont.FontFamily, (int)markerFont.Style, symbolSize.Height * 0.9f, CorrectedSymbolLocation, stringFormat);
graphics.FillPath(brush, p);

我已经尝试过一些东西,比如:

Region region = new Region(p);
graphics.FillRegion(new SolidBrush(Color.White), region);

我在互联网上搜索了一下,我发现了一个提到函数的页面:graphicsPath.Outline()(所以在我的例子中是p.Outline()),但是C#不能识别这个函数。

有人可以告诉我,我想尝试达到的目标是否可能,如果可以,我该如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

我终于找到了一个解决方案,感谢博客文章Here!

我没有创建位图,而是根据博客中的建议创建GraphicsPathIterator

我现在添加GraphicsPathIterator,而不是仅创建路径并填充路径(如问题中所述的代码),我现在添加GraphicsPath p = new GraphicsPath(); p.AddString(MarkerSymbol, markerFont.FontFamily, (int)markerFont.Style, symbolSize.Height * 0.9f, CorrectedSymbolLocation, stringFormat); var iter = new GraphicsPathIterator(p); while (true) { var subPath = new GraphicsPath(); bool isClosed; if (iter.NextSubpath(subPath, out isClosed) == 0) break; Region region = new Region(subPath); graphics.FillRegion(new SolidBrush(Color.White), region); } graphics.FillPath(brush, p);

height:100%