Working Pixel to Twips conversion?

Just starting out? Need help? Post your questions and find answers here.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Working Pixel to Twips conversion?

Post 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?
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Working Pixel to Twips conversion?

Post 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.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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 :)
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

NOTE THAT 15 IS WRONG since it is hardcoded, use getdevicecaps, see above..
deadmoap
User
User
Posts: 79
Joined: Sun Feb 22, 2004 11:45 pm
Location: Riverdale, Utah
Contact:

Post 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 ^_^
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Note that value IS a float.
deadmoap
User
User
Posts: 79
Joined: Sun Feb 22, 2004 11:45 pm
Location: Riverdale, Utah
Contact:

Post 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.
Post Reply