By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Properly Sizing a Bitmap

P: 2
All,

I am trying to create a bitmap dynamically based on a selection of text. Code is below:
Expand|Select|Wrap|Line Numbers
  1. bitMapImage = new
  2.                 System.Drawing.Bitmap(90,50);
  3.  
  4.       Graphics graphicImage = Graphics.FromImage(bitMapImage);
  5.  
  6.             graphicImage.Clear(Color.White);
  7.             SolidBrush drawBrush = new SolidBrush(Color.Red);
  8.  
  9.             graphicImage.DrawString(comboStampText.Text +  DateTime.Now.ToShortDateString(),
  10.                new Font("Arial", 18, FontStyle.Regular),
  11.                drawBrush, new Point(5, 5));
  12.  
Everthing works except I am having difficulty sizing the bitmap correctly based on the length of the text. Is there a way that this can be accomplished?

Regards,

Scott
Feb 4 '09 #1
Share this Question
Share on Google+
1 Reply


vekipeki
Expert 100+
P: 229
You can use System.Graphics.MeasureString(text, font) to get the size of your string for that graphics instance - but it also has a couple of things you should know before using it, check this link for an example: http://www.codeproject.com/KB/GDI-pl...urestring.aspx

There is also the System.Windows.Forms.TextRenderer class, which behaves a bit different when rendering (it uses plain GDI instead of GDI+). In some cases it is a better solution (for example, when rendering semi-transparent text to a bitmap, it does a better job with anti-aliasing).
Feb 5 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.