Wednesday, 15 May 2013

gdi+ - How to measure the pixel width of a digit in a given font / size (C#) -


I am trying to calculate the pixel width of the Excel column, like using the official formula from the OpenXML specification Is described. However, in order to apply this formula, I need to know the max numeric width of the normal font, which is the pixel width of the broad numerical digits. The OpenXML specification gives this example as an explanation:

Using the Calibri font as an example, the maximum digit width of 11-digit font size is 7 pixels (96 dpi). .

I have checked that it is correct by examining a calibrary 11-point digit with the correct view and it is actually 7 pixels wide. Therefore, I am trying to create a method which will return the maximum number of widths for any font / size.

I have followed the recommendations given in me, but it expects that the result does not arise. / P>

Here is my test code:

  var font = new font ("calibrary", 11.0f, font style. Regular); (Debug.WriteLine (TextRenderer.MeasureText (i.ToString (), Font));}  

for this (Var i = 0; i & lt; 10; i ++) It tells that the width of all the digits is 15.

Any suggestions please?

thanks, Tim

First let me know that the code I am showing looks like a terrible hack and I'm not presenting it as one of the measures. Instead, I'm hoping I am giving this information a bit more about the behavior of the measurement text

I have made a slight change to your code. I am measuring the length of two underscore characters, after that in the body of the loop, I underscore the desired character on both sides I am scaling down and reducing the length of two underscores alone. For the test scenario, I am getting the result of 7. Its main objective was to determine that the measurement text is a letter-rate -Description for padding on the class basis or whether the padding on the beginning / end of the string It seems that this is the latter. It is the latter. Perhaps it will inspire you with a more elegant solution, combined with some other inputs you have received.

  var font = new font ("calibrari", 11.0f, font style. Regular); Intro underscore width = TextRenderer.MeasureText ("__", font). With; (Var i = 0; i & lt; 10; i ++) {int charWidth = TextRenderer.MeasureText (String.Concat ("_", i.ToString (), "_"), font). With - underscore width; Debug.WriteLine (charWidth); }  

No comments:

Post a Comment