Solved

System.Drawing font with background color

Posted on 2007-11-21
5
3,587 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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