Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the ratio between Twips and Pixels? 3

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
0
0
US
I have a TON of vb6.TwipsToPixels() functions in my upgraded program that I'd like to get rid of. What's the conversion factor?
 
I think one twip = 1400 pixels....
 
No, I think it's 1440 twips to the inch. The ratio of twips to pixels isn't quite that large.

But I'll keep looking...
 
Shelton is right, it is 15 twips to the pixel.

I tested it by building a simple App in VB6 and set the height and width of a command button in twips. Saved it and then opened the project in .NET, which upgraded it and put in the VB6.TwipsToPixelsX/Y conversion. I then added a couple of labels to display the twips value and then show the actual Pixel size from the commond button directly. Answer was right there, 15:1 ratio of Twips to Pixels.

Thanks for the help though. Nice to have independent confirmation. Strange this doesn't appear anywhere in the Help text...
 
The ratio of 15:1 applies in most cases, however the number of pixels per inch can vary. It is much safer to determine the appropriate ratio to use at runtime than it is to use a constant. This used to be done through a series of API calls in the VB6 days, but can now be done with the following code using the Dots Per Inch property of the Graphics Object.
Code:
Private Function ConvertTwipsToPixels(_
  ByVal twips As Long, _ 
  ByVal isHorizontal As Boolean) As Long 
    Const TWIPSPERINCH As Long = 1440 
    Dim gr As Graphics 
    
    gr = Me.CreateGraphics 
    If isHorizontal Then 
      Return = CLng(lngTwips / TWIPSPERINCH * gr.DpiX) 
    Else 
      Return = CLng(lngTwips / TWIPSPERINCH * gr.DpiY) 
    End If 
End Function

Private Sub TestFunction()
    Dim myResult as Long    

    ' Convert Horizontal Measurement
    myResult = ConvertTwipsToPixels(1440, True)

    ' Convert Vertical Measurement
    myResult = ConvertTwipsToPixels(1440, False)
End Sub
This code was adapted from an example on a thread at the following url
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top