Solved

How do I set the size for Graphics DrawString

Posted on 2009-05-15
3
2,403 Views
Last Modified: 2013-12-17
Refer to code snippet. The Bitmap Size is set as follows:  Bitmap(500, 50);
The string I am drawing is DateTime.Now.ToShortTimeString()
I want to auto-resize my bitmap canvas/imagesize to allow for the actual lentgh of my string.
If I replace DateTime.Now.ToShortTimeString() with VeryLongTextString (unknown length)
eg. g.DrawString(VeryLongTextString, new Font("Arial", 32), Brushes.Red, new PointF(0, 0));
How do I Get around the limitations set by Bitmap(500, 50);
I don't want to necessarily resize or stretch the image, but instead change the canvas or image size to match the "content"
Bitmap bmp = new Bitmap(500, 50);

Graphics g = Graphics.FromImage(bmp);

g.FillRectangle(Brushes.White, 0, 0, 500, 50);

g.DrawString(DateTime.Now.ToShortTimeString(), new Font("Arial", 32), Brushes.Red, new PointF(0, 0));

Open in new window

0
Comment
Question by:andrejonker
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
tculler earned 200 total points
ID: 24397590
You can use g.MeasureString(String, Font). This returns a SizeF object (a Size with float dimensions instead of integer). Just remember that you need to store the String beforehand; otherwise, two consecutive calls to DateTime.Now.ToShortTimeString() will yield differetn results. Here's an example, expanding on your code, presented in the following code snippet.

Hope it helps,
Nate
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(500, 50);

System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(bmp);
 

System.String shortDate = System.DateTime.Now.ToShortDateTimeString();

System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 32F);
 

gfx.Clear(System.Drawing.Color.White); // This fills the whole bitmap with white, instead of manualy filling a rectangle

gfx.DrawString(shortDate, drawFont, System.Drawing.Brushes.Red, new System.Drawing.PointF(0f, 0f));

System.Drawing.SizeF dateSize = gfx.MeasureString(shortDate, drawFont);

Open in new window

0
 
LVL 5

Assisted Solution

by:gazzzzzzer
gazzzzzzer earned 300 total points
ID: 24404106
I agree that the MeasureString method provided by the Graphics Class should help you with your problem.

I have included an example to show how you could use this and increase the Canvas size as needed.
            Bitmap bmp = new Bitmap(500, 50);

            Graphics g = Graphics.FromImage(bmp);

            

            string shortDate = DateTime.Now.ToShortDateString();

            Font drawFont = new Font("Arial", 32F);

            SizeF dateSize = g.MeasureString(shortDate, drawFont);
 

            bmp = new Bitmap(bmp, Convert.ToInt32(dateSize.Width), Convert.ToInt32(dateSize.Height));

            g = Graphics.FromImage(bmp);
 

            g.FillRectangle(Brushes.White, 0, 0, 500, 50);

            g.DrawString(shortDate, drawFont, Brushes.Red, new PointF(0, 0));
 

// Continue to use the g and bmp param as required

Open in new window

0
 
LVL 5

Assisted Solution

by:gazzzzzzer
gazzzzzzer earned 300 total points
ID: 24404123
Sorry , you do not need or unlikely to want the line : g.FillRectangle(Brushes.White, 0, 0, 500, 50);

Here is the updated code:
            Bitmap bmp = new Bitmap(500, 50);

            Graphics g = Graphics.FromImage(bmp);

            

            string shortDate = DateTime.Now.ToShortDateString();

            Font drawFont = new Font("Arial", 32F);

            SizeF dateSize = g.MeasureString(shortDate, drawFont);

 

            bmp = new Bitmap(bmp, Convert.ToInt32(dateSize.Width), Convert.ToInt32(dateSize.Height));

            g = Graphics.FromImage(bmp);

 

            g.DrawString(shortDate, drawFont, Brushes.Red, new PointF(0, 0));

 

// Continue to use the g and bmp param as required

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl help with selection 14 44
C# Error - Add Failed 12 53
c# if statement weird reaction 3 30
install app on other machine 13 16
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

11 Experts available now in Live!

Get 1:1 Help Now