Solved

System.Drawing font with background color

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

18 Experts available now in Live!

Get 1:1 Help Now