Solved

System.Drawing font with background color

Posted on 2007-11-21
5
3,368 Views
Last Modified: 2013-12-16
I have some in a class that puts text onto an image.  I want the ability to put a solid background behind the text but I'm having trouble getting the width to be the correct width (it's always too long).  I've tried change the unit and can't get it quiet right, anyone know what I'm missing (which is probably something simple)?  Or, another way to phrase the question is, how do I get the length of the text in a format that FillRectangle matches up with?  My snippet is in VB but I'll take either VB or C# suggestions, language is irrelevant for my purposes.
Dim Image As New Bitmap(_fileName)

Dim Gr As Graphics = Graphics.FromImage(Image)

Dim Brush As New System.Drawing.SolidBrush(_color)

Gr.FillRectangle(Brushes.Blue, _x, _y, (_font.SizeInPoints * _text.Length), _font.Height)

Gr.DrawString(Text, _font, Brush, _x, _y)

Open in new window

0
Comment
Question by:IUFITS
  • 2
  • 2
5 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 450 total points
ID: 20330133
Use MeasureString:
Dim Image As New Bitmap(_fileName)

Dim Gr As Graphics = Graphics.FromImage(Image)

Dim Brush As New System.Drawing.SolidBrush(_color)

Dim rectSize As SizeF = Gr.MeasureString(Text, _font)

Gr.FillRectangle(Brushes.Blue, _x, _y, rectSize.Width, rectSize.Height)

Gr.DrawString(Text, _font, Brush, _x, _y)

Open in new window

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20330158
use MeasureString first:

Dim size As SizeF = Gr.MeasureString(Text, _font)

Then draw the rectangle:

Gr.FillRectangle(Brushes.Blue, _x, _y, size.Wdith, size.Height)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20330166
Sorry, too slow typing.
0
 
LVL 3

Assisted Solution

by:wizrr
wizrr earned 50 total points
ID: 20330374
You can use MeasureCharacterRanges.
http://www.developingstorm.com/2005/12/net-headache-measurestring.php

You also can use System.Windows.Forms.TextRenderer - text drawn/measured by this class is more accurate and fast.

Sometimes when you measuring string with spaces and same symbols "          aaaaaa" and "          aaaaaaaaaaaaaaaaaaaaaa" using MeasureString or MeasureCharacterRanges - you can get different measurement  of same character ranges (but this two strings with range from 2nd character to 16nd character is same) - this is known issue in .NET 1.0\1.1. Solution is to use StringFormat.GenericTypographic and Graphics.TextRenderingHint == TextRenderingHint.AntiAliasGridFit . TextRenderer is new in .NET 2.0 and can solve this problem also, but you can't paint gradiented text with it.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20330537
The tiny extra space in MeasureString is fine for this application.  It adds space in the rectangle around the text.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now