0

I have a project in VB6 that I need to convert to C#, this project uses the TextWidth Method and I replaced it with TextRenderer.MeasureText Method in C# but I got different results for the same strings with the same fonts even after i converted the pixels to twips, does anyone know how to fix this?
my code in VB6:

    Private Sub Form_Load()
      MsgBox Me.TextWidth("XX")
    End Sub

the output: 210

my code in C#:

  public int ConvertPixelsToTwips(Graphics source, int pixel)
    {

        return (int)((double)pixel / ((1.0 / 1440.0) * source.DpiX));   
    }


    private void Form1_Load(object sender, EventArgs e)
    {


        int pixel = TextRenderer.MeasureText("XX",this.Font).Width;
        int twips;
        Graphics g = CreateGraphics();
        twips = ConvertPixelsToTwips(g, pixel);
        MessageBox.Show("in twips: " + Convert.ToString(twips) + " in pixel: " + pixel);

    }

the output: in twips: 315 in pixel: 21

user2109003
  • 5
  • 1
  • 3
  • I'm going to ask again, just because you seem to be *so* focused on getting this conversion sorted out - how does the original code then go on to *use* the twips value? Because I'm almost certain that *that* code is going to have to be changed also - are you not just jumping through a lot of hoops to get the pixel to twips conversion right, only to then have to turn around and convert twips values back to pixels? – Damien_The_Unbeliever Dec 18 '13 at 07:07
  • Answered here, http://stackoverflow.com/q/4044397/17776 – jac Dec 18 '13 at 16:32

0 Answers0