2

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.

Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
  • with gdi+, you can _measure_ the string. so you can check where the next character must appear by measuring the string cut up to that character. then, you can output the characters one by one. or you can use Graphics.MeasureCharacterRanges to get the position of each character – Vlad Oct 31 '12 at 17:57
  • I have tried using MeasureCharacterRanges, but I can't seem to draw each character using those regions. If I draw each character using DrawString in the region it's supposed to go in, the region is too small and clips the character. – Trevor Elliott Oct 31 '12 at 20:18
  • Edited my question with my results. – Trevor Elliott Oct 31 '12 at 20:31

1 Answers1

2

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.

  • I used StringAlignment.Center when drawing each character in the rectangles provided by MeasureCharacterRanges, and that worked. – Trevor Elliott Nov 01 '12 at 14:18