# Calculate text lenth in pixel with different font size

• ### Question

• User-1558868557 posted

Hi

Is it possible to calculate text length in pixel according to different size.

Any help will be appreciated.

Thank you.

Tuesday, May 26, 2009 7:15 AM

• User187056398 posted

Yes.  In the following code, lines 26 and 27 measure the text using the selected font:

```1        /// ---- TextToBitmap ----------------------------
2        ///
3        /// using System.Drawing;
4        /// using System.Drawing.Imaging;
5        /// Author: Steve Wellens
6        ///
7        /// <summary>
8        /// Takes a Text string and returns a bitmap of the string
9        /// </summary>
10
11       public static Bitmap TextToBitmap(String TheText)
12       {
13           Font DrawFont = null;
14           SolidBrush DrawBrush = null;
15           Graphics DrawGraphics = null;
16           Bitmap TextBitmap = null;
17           try
18           {
20               // and choose a font
21               TextBitmap = new Bitmap(1, 1);
22               DrawGraphics = Graphics.FromImage(TextBitmap);
23               DrawFont = new Font("Arial", 16);
24
25               // see how big the text will be
26               int Width = (int)DrawGraphics.MeasureString(TheText, DrawFont).Width;
27               int Height = (int)DrawGraphics.MeasureString(TheText, DrawFont).Height;
28
29               // recreate the bitmap and graphic object with the new size
30               TextBitmap = new Bitmap(TextBitmap, Width, Height);
31               DrawGraphics = Graphics.FromImage(TextBitmap);
32
33               // get the drawing brush and where we're going to draw
34               DrawBrush = new SolidBrush(Color.Black);
35               PointF DrawPoint = new PointF(0, 0);
36
37               // clear the graphic white and draw the string
38               DrawGraphics.Clear(Color.White);
39               DrawGraphics.DrawString(TheText, DrawFont, DrawBrush, DrawPoint);
40
41               return TextBitmap;
42           }
43           finally
44           {
45               // don't dispose the bitmap, the caller needs it.
46               DrawFont.Dispose();
47               DrawBrush.Dispose();
48               DrawGraphics.Dispose();
49           }
50       }
51
```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, May 26, 2009 10:23 AM

### All replies

• User187056398 posted

Yes.  In the following code, lines 26 and 27 measure the text using the selected font:

```1        /// ---- TextToBitmap ----------------------------
2        ///
3        /// using System.Drawing;
4        /// using System.Drawing.Imaging;
5        /// Author: Steve Wellens
6        ///
7        /// <summary>
8        /// Takes a Text string and returns a bitmap of the string
9        /// </summary>
10
11       public static Bitmap TextToBitmap(String TheText)
12       {
13           Font DrawFont = null;
14           SolidBrush DrawBrush = null;
15           Graphics DrawGraphics = null;
16           Bitmap TextBitmap = null;
17           try
18           {
20               // and choose a font
21               TextBitmap = new Bitmap(1, 1);
22               DrawGraphics = Graphics.FromImage(TextBitmap);
23               DrawFont = new Font("Arial", 16);
24
25               // see how big the text will be
26               int Width = (int)DrawGraphics.MeasureString(TheText, DrawFont).Width;
27               int Height = (int)DrawGraphics.MeasureString(TheText, DrawFont).Height;
28
29               // recreate the bitmap and graphic object with the new size
30               TextBitmap = new Bitmap(TextBitmap, Width, Height);
31               DrawGraphics = Graphics.FromImage(TextBitmap);
32
33               // get the drawing brush and where we're going to draw
34               DrawBrush = new SolidBrush(Color.Black);
35               PointF DrawPoint = new PointF(0, 0);
36
37               // clear the graphic white and draw the string
38               DrawGraphics.Clear(Color.White);
39               DrawGraphics.DrawString(TheText, DrawFont, DrawBrush, DrawPoint);
40
41               return TextBitmap;
42           }
43           finally
44           {
45               // don't dispose the bitmap, the caller needs it.
46               DrawFont.Dispose();
47               DrawBrush.Dispose();
48               DrawGraphics.Dispose();
49           }
50       }
51
```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Tuesday, May 26, 2009 10:23 AM
• User-1558868557 posted

Hello,

SGWellens

Thank you very much [:)]

Wednesday, May 27, 2009 3:19 AM