Exact width of a font character

M

meicher

Hi,
I want to get the EXACT width of a font character in pixels.

I tried:

Font fo = new Font("Courier New", 12, GraphicsUnit.Pixels);
int iWidth = (int)fo.Size; // 12 doesn't work
iWidth = (int)MeasureString("G", fo).Width; //doesn't work

:(

Regards mike
 
M

Morten Wennevik

Well, there is no definition for GraphicsUnit.Pixels, you need to use
GraphicsUnit.Pixel (no s at the end).
You can't simply call MeasureString. You need a graphics object first.

Font fo = new Font("Courier New", 12, GraphicsUnit.Pixel);
int iWidth = (int)fo.Size; // == 12;
Graphics g = this.CreateGraphics();
iWidth = (int)g.MeasureString("G", fo).Width; // == 11
g.Dispose();

Casting a float to int in this case will also remove quite a bit of
precision since "G" would be 11.4 pixels wide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top