Search code examples
c#system.drawingdrawstring

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


I'm working on a project that makes it possible to draw objects on a map in c#. We make use of our own created True Typed Font (.ttf). The reason we do this is because users must be able to specify their own icons for objects if they want.

The font (letter) I'm currently drawing is a outlined map-marker as you can see in the image

Map markesr

The numbers are drawn later, but as you can see this isn't clear enough because of the background.

What I now want to do is to fill the marker white, instead of being transparent.

I draw the marker the following way:

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

I've already tried some things, like:

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

I searched on the internet and I found a page mentioning a function: graphicsPath.Outline() (so in my case p.Outline()), but C# doesn't recognize this function.

Can someone tell me if it's possible what I want to try to reach, and if so, how I can achieve this?


Solution

  • I finally found a solution, thanks to a blogpost found Here!

    Instead of creating a bitmap, I create, as suggeste in the blog, a GraphicsPathIterator.

    Instead of only creating the path and fill the path (as the code said in the question), I now add the 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);