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

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);
  4.       Graphics graphicImage = Graphics.FromImage(bitMapImage);
  6.             graphicImage.Clear(Color.White);
  7.             SolidBrush drawBrush = new SolidBrush(Color.Red);
  9.             graphicImage.DrawString(comboStampText.Text +  DateTime.Now.ToShortDateString(),
  10.                new Font("Arial", 18, FontStyle.Regular),
  11.                drawBrush, new Point(5, 5));
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?


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

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:

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.