How to paint a bold text and normal text
-
Saturday, January 02, 2010 1:58 AMPlease help me to make bold some characters in sentence by using Graphics object. Thanks.
Example: I want to make bold on "example" word:
This is an example text.
Please remember to keep the spacing between characters.
All Replies
-
Saturday, January 02, 2010 2:32 AM
Graphics.DrawString("SampleText", new Font("Arial", 80, FontStyle.Bold), Brushes.Black, 150, 125);
http://msdn.microsoft.com/en-us/library/aa984399%28VS.71%29.aspx -
Saturday, January 02, 2010 2:34 AMHi,
You can specify bold for font style when you initialize font object by passing fontstyle parameter before you call the method of DrawString of Graphics to draw a text on form.
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16, FontStyle.Bold );
Please check the following link for details:
http://msdn.microsoft.com/en-us/library/9why95hd.aspx
Hi, I am Kyi. -
Saturday, January 02, 2010 2:36 AMPlease give me any example. I have problem about define spacing between bold text & normal text.
Please help me. -
Saturday, January 02, 2010 2:48 AM
Something like this should do what you're after:
private void Form1_Paint(object sender, PaintEventArgs e) { using (Font normal = new Font("Tahoma", 10, FontStyle.Regular)) using (Font bold = new Font("Tahoma", 10, FontStyle.Bold)) using (StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone()) { format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; Rectangle temp = ClientRectangle; SizeF size = e.Graphics.MeasureString("This is an ", normal, temp.Width, format); e.Graphics.DrawString("This is an ", normal, SystemBrushes.WindowText, temp, format); temp.X += (int)size.Width; size = e.Graphics.MeasureString("example ", bold, temp.Width, format); e.Graphics.DrawString("example ", bold, SystemBrushes.WindowText, temp, format); temp.X += (int)size.Width; e.Graphics.DrawString("text.", normal, SystemBrushes.WindowText, temp, format); } } -
Saturday, January 02, 2010 3:09 AMPlease test your code with the following text:
This is anexampletext.
I see it has a remaining space after "example".
-
Saturday, January 02, 2010 6:12 AM
Sorry, it worked for me, but I forgot MeasureString will not produce the expected results given some settings, this code should work regardless (at least, it does for me on Windows 7 and XP):
private void Form1_Paint(object sender, PaintEventArgs e) { using (Font normal = new Font("Tahoma", 10, FontStyle.Regular)) using (Font bold = new Font("Tahoma", 10, FontStyle.Bold)) using (StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone()) { format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; Rectangle temp = ClientRectangle; DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "This is an ", normal); DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "example ", bold); DrawString(e.Graphics, SystemBrushes.WindowText, ref temp, format, "string.", normal); } } void DrawString(Graphics g, Brush brush, ref Rectangle rect, StringFormat format, string text, Font font) { using (StringFormat copy = (StringFormat)format.Clone()) { copy.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, text.Length)}); Region[] regions = g.MeasureCharacterRanges(text, font, rect, copy); g.DrawString(text, font, brush, rect, format); int width = (int)(regions[0].GetBounds(g).Width); rect.X += width; rect.Width -= width; } }- Marked As Answer by Le Vo Quang Saturday, January 02, 2010 11:10 AM

