是否可以在c#中用GraphicsClass填充一个字母?

Is it possible to fill a letter with the Graphics Class in c#?

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

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

数字是稍后绘制的,但是如您所见,由于背景原因不够清晰。

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

我按以下方式绘制标记:

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

我在 Internet 上搜索并找到了一个提到函数的页面:graphicsPath.Outline()(所以在我的例子中是 p.Outline()),但是 C# 不识别这个函数。

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

一种可能的解决方法是

1 - 创建大小合适的 Bitmap

2 - DrawString 到它

3 - 使用 floodfill 填充内部。

然后使用 DrawImage 而不是 DrawString

对于 FontSize 收集 位图中的每个更改,您将必须 重复 此操作..

我不确定这个(或我能想到的任何其他解决方案)对任意字体字形的处理效果如何。特别是当它们 open 时,没有明确定义您想要看到的内容..

对于每个角色,您都需要一个或多个位于内部的点。

我终于找到了解决方案,多亏了一篇博文 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);