Page 1 of 1

Working Pixel to Twips conversion?

Posted: Thu Aug 05, 2004 5:40 am
by Shannara
Ive been looking at code all day long, and have not found a real working conversion code for converting Pixel to Twips...

Some people claim PIXELS * 15, or PIXELS * 15.5, however that is 100% fake. Try the above with a 1440x900x32 resolution, the result is always wrong...

Anybody have a real conversion code for this?

Posted: Thu Aug 05, 2004 5:54 am
by MadMax

Posted: Thu Aug 05, 2004 6:00 am
by Shannara
Thanks for the info, but the MSDN is wrong. The results for height and width are still off by several (usually hundred) twips on a 1440x900x32 resolution.

Re: Working Pixel to Twips conversion?

Posted: Thu Aug 05, 2004 6:20 am
by PB
> Some people claim PIXELS * 15

That's how I knew it. A Visual Basic form that uses twips for its form size is
the above equation, and that's a fact. I never knew twips to be anything else.

Posted: Thu Aug 05, 2004 6:40 am
by Shannara
Not a fact, try the following code in VB:

my resolution:1440x900x32
Take a form and make the width 5130, make sure the scale is TWIPS. Please note that 5130 Twips = 336 Pixels....
Quote:
Result = (336 * Screen.TwipsPerPixelX)
Debug.Print Result
The result = 5040 TWIPS. This is 100% wrong.

Posted: Thu Aug 05, 2004 7:10 am
by Shannara
NVM, turns out that the conversion are for the client area and does not include the random titlebar height and border sizes. Stupid MS, ah well, all fixed out, thx anyways :)

Posted: Thu Aug 05, 2004 8:46 am
by Edwin Knoppert
twips per pixel = 1440 / DPI
Like 1440 / 120 = 12
Since 1440 twips FIT in an inch
andf there are 120 pixels to FIT in your inch for the current DC.

So:
TPPX = 1440 / GetDeviceCaps( LOGPIXELSX )
TPPY = 1440 / GetDeviceCaps( LOGPIXELSY )

So 2 cm in pixels will be
pixelsx = ( 567 * 2 ) / TPPX

Posted: Thu Aug 05, 2004 9:44 am
by PB
> Not a fact

Sorry, my mistake: I meant that twips/15 = pixels (not *15). I opened VB
and made a form whose width is 6300 twips, and height is 5100 twips. Divide
these by 15 and you get 420 pixels wide and 340 pixels high. If you check
this form with any window viewer, these pixels dimensions are correct.
So, for all intents and purposes, twips/15 = pixels (in my experience/tests).

UPDATE: Sorry (again) -- I just realised you're trying to convert pixels to
twips, not twips to pixels. :lol: I've never had to do that, so I don't know.

Posted: Thu Aug 05, 2004 10:04 am
by Edwin Knoppert
NOTE THAT 15 IS WRONG since it is hardcoded, use getdevicecaps, see above..

Posted: Fri Aug 06, 2004 5:55 pm
by deadmoap
Here are two functions to get the current Twips per pixel:

Code: Select all

#LOGPIXELSX=88
#LOGPIXELSY=90

Procedure GetTwipsPerPixelX()
  hDC=GetDC_(GetDesktopWindow_())
  TwipsPerPixelX=1440/GetDeviceCaps_(hDC,#LOGPIXELSX)
  ReleaseDC_(GetDesktopWindow_(),hDC)
  ProcedureReturn TwipsPerPixelX
EndProcedure

Procedure GetTwipsPerPixelY()
  hDC=GetDC_(GetDesktopWindow_())
  TwipsPerPixelY=1440/GetDeviceCaps_(hDC,#LOGPIXELSY)
  ReleaseDC_(GetDesktopWindow_(),hDC)
  ProcedureReturn TwipsPerPixelY
EndProcedure
I hope my 1337 pr0gr4mm1ng skillz helped ^_^

Posted: Fri Aug 06, 2004 7:26 pm
by Edwin Knoppert
Note that value IS a float.

Posted: Fri Aug 06, 2004 8:45 pm
by deadmoap
Edwin Knoppert wrote:Note that value IS a float.
Four decimal points off either way isn't going to ruin the whole picture.