Solved

System.Drawing font with background color

Posted on 2007-11-21
5
3,709 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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