2

I want to draw string in a Bitmap like this:

Font myfont=new Font("TimesNewRoman",18)
Bitmap bmpBitmap =new Bitmap(200,100);
Graphics g=Graphics.FromImage(bmpBitmap);
g.DrawString("SampleText",myfont,Brushes.Black);

How do I determine the size of bitmap?

andreister
  • 13,333
  • 2
  • 41
  • 43
mohammad
  • 1,187
  • 2
  • 14
  • 27

2 Answers2

3

Use the following function to measure the size of a string with respect to a certain Font.

SizeF Graphics.MeasureString(string text, Font font)

Also, ensure the TextRenderingHint for the Graphics is set to AntiAlias. This makes a world of difference when it comes to the readability of the text.


If you want to measure a string before you create your Bitmap, use the solution presented over here: https://stackoverflow.com/a/1003503/1828879

Community
  • 1
  • 1
Timothy Shields
  • 70,640
  • 17
  • 114
  • 164
0

Have you tried using bmpBitmap.Size to get both Height and Width, or bmpBitmap.Height and bmpBitmap.Width to get one or the other?

Xynariz
  • 1,221
  • 1
  • 10
  • 28