Question

I want to use Graphics.DrawString to draw characters using individual colors.

The problem is that each time I call DrawString, I have to know where to position it (either using a Point or a Rectangle) relative to the other characters.

This is almost possible when taking into account the kerning and wrapping that happens when you use DrawString in a rectangle with a StringFormat. The StringFormat is important to specify alignment, wrapping, trimming, etc.

Ideally I'd be able to tell DrawString how to color each character individually. The other option is if I can measure a string in a rectangle using a StringFormat and then retrieve the X and Y positions of each character index, so that I can draw each one with its own brush.

I tried to measure each character individually, but that will not work because a combination of characters in a string is not the same width as each character measured on its own.

EDIT: Here is what is happening when I take a string, get the Region array, fill those regions with red and blue alternating, and then try to draw each individual character in those regions:

enter image description here

I am calling DrawString as follows:

g.DrawString(character, font, brush, region.GetBounds(g));

Also tried the following:

Rectangle bounds = Region.GetBounds(g);
g.DrawString(character, font, brush, new PointF(bounds.Left, bounds.Top));

And,

g.DrawString(character, font, brush, region.GetBounds(g), stringFormat);

Same results.

Was it helpful?

Solution

GDI+'s DrawString is padding the result which explains the left offset you get.

Try to use the GDI to measure the string. Luckily this is implemented and easy to reach from .Net through the TextRenderer() object, f.ex:

Size sz = TextRenderer.MeasureText(character, 
                                   font, 
                                   new Point(Integer.MaxValue, Integer.MaxValue),
                                   TextFormatFlags.NoPadding);

You can also look into the TextRenderer.DrawText().

Of course, you can always modify your StringFormat instance to use center alignment and DrawString with a rectangle instead of point.

StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
Rectangle cr = new Rectangle(x, y, w, h); //same as the colored region
g.DrawString(character, font, brush, cr, sf);

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top