Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

System.Drawing font with background color

Posted on 2007-11-21
5
Medium Priority
?
3,823 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 1800 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 200 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

671 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