Page 1 of 1

Converting metric units into pixels????????

Posted: Mon Apr 06, 2009 6:54 pm
by Peter_DK
Hi guys.

I'm in dire need for help here. For some reason I simply cannot find the solution to my problem. I hope you can help me

I need to convert a set of numbers into its equivalent pixels.
(Screen/Window based, not printer based)

Example: 12.4 cm <---> ??? pixels.
8.35 cm <---> ??? pixels

How can I do this?
Calculating how many pixels equals a centimeter simply eludes me completely..

Thank you in advance

Best regards
Peter

Posted: Mon Apr 06, 2009 7:01 pm
by Kaeru Gaman
"pixel" is no reallife dimension unit.
thus you surely need a reference value to calculate this.
this is what dpi is for, this means "dots per inch" (http://en.wikipedia.org/wiki/Dots_per_inch)
and determines how many pixel make up one inch.
usual values of a display is 96 dpi or 120 dpi, according to your systems settings.
this means a printout represent, and has nothing to do with the size of your monitor.

without this reference there is no way to tell how many pixel make up one centimeter,
regardless how many questionmarks one my use.

Posted: Mon Apr 06, 2009 7:12 pm
by Peter_DK
Hehe.. Thank you for your reply :D

The questionmarks were placed there to keep me from hitting my monitor :)

I've been fumbling with this for 6 hours straight.

Anyway.. Thank you for your reply.


Best regards
Peter.

Posted: Mon Apr 06, 2009 7:31 pm
by Arctic Fox
If you're using Windows, this may also be of interest for you
http://www.purebasic.fr/english/viewtopic.php?t=2914

And a slightly modified version :)

Code: Select all

hdc = GetDC_(GetDesktopWindow_())
If hdc : dpiX.l = GetDeviceCaps_(hdc, #LOGPIXELSX) : ReleaseDC_(GetDesktopWindow_(), hdc) : EndIf

Debug "Desktop resolution: " + Str(dpiX) + " dpi"
Debug "Pixels per cm: " + StrD(dpiX / 2.54)
End

Posted: Mon Apr 06, 2009 7:33 pm
by luis
Something like this maybe:

Code: Select all

EnableExplicit

Procedure.f CmToPixHorz (fCM.f)
 
 Protected hDC, hWinDesk
 Protected fSizeCm.f, fSizePix.f, fPixPerCm.f
 Protected fResult.f = 0.0
 
 ; get desktop window handle   
 hWinDesk = GetDesktopWindow_()
 
 ; get deskop window DC
 hDC = GetDC_(hWinDesk) 

 If hDC
    ; size of screen along X in CM 
    fSizeCm.f = GetDeviceCaps_(hDC, #HORZSIZE) / 10
    ; size of screen along X in pixels
    fSizePix.f = GetDeviceCaps_(hDC, #HORZRES) 
    
    ;pixel per CM
    fPixPerCm = (fSizePix / fSizeCm)
    
    ReleaseDC_(hWinDesk, hDC) 
    
    ; converts CM to pixels
    fResult = (fPixPerCm * fCM)
 EndIf
 
 ProcedureReturn fResult
 
EndProcedure

Procedure.f CmToPixVert(fCM.f)
 
 Protected hDC, hWinDesk
 Protected fSizeCm.f, fSizePix.f, fPixPerCm.f
 Protected fResult.f = 0.0
 
 ; get desktop window handle   
 hWinDesk = GetDesktopWindow_()
 
 ; get deskop window DC
 hDC = GetDC_(hWinDesk) 

 If hDC
    ; size of screen along Y in CM 
    fSizeCm.f = GetDeviceCaps_(hDC, #VERTSIZE) / 10
    ; size of screen along Y in pixels
    fSizePix.f = GetDeviceCaps_(hDC, #VERTRES) 
    
    ;pixel per CM
    fPixPerCm = (fSizePix / fSizeCm)
    
    ReleaseDC_(hWinDesk, hDC) 
    
    ; converts CM to pixels
    fResult = (fPixPerCm * fCM)
 EndIf
 
 ProcedureReturn fResult
 
EndProcedure

Debug CmToPixHorz (37.5)
Debug CmToPixVert (30.0)

My screen is 1280x1024 and 37.5x30 cm

I would say it's a match.

Posted: Mon Apr 06, 2009 7:39 pm
by srod
Better off using the logical dpi as with Arctic Fox's example. #HORZSIZE etc. is notoriously dodgy and can often be reported incorrectly by the display driver.

On a printer, logical dpi equates to physical dpi so it's win win! :)

Posted: Mon Apr 06, 2009 7:41 pm
by Peter_DK
Oh..my.... snifff! Thank you..thank you...thank you....!!

I owe you guys a debt of gratitude!!
What a bunch of lifesavers..
You've made my day :)

This is precisely what i was looking for.
Best regards

Peter

Posted: Mon Apr 06, 2009 7:52 pm
by luis
srod wrote:Better off using the logical dpi as with Arctic Fox's example. #HORZSIZE etc. is notoriously dodgy and can often be reported incorrectly by the display driver.
That would have been my first solution, but I discovered it doesn't work so well. Using that code my screen width results in 1418 pixels for 37.5 cm while in reality is 1280.

With the code I posted I get 1280.

Posted: Mon Apr 06, 2009 8:01 pm
by srod
Logical dpi is usually greater than the physical measurements, but then this is the whole point of using logical dpi. Most applications will use logical dpi where appropriate.

Posted: Mon Apr 06, 2009 8:35 pm
by Peter_DK
I see how volatile the results can be. But if you've
seen my calculations.Arctic Fox and Luis's examples gives far better results.
Mine calculations weren't even close :oops:

After 6 hours of trial and error and error and error, my brain turned into a dark damp void, only occupied by a single teeny tiny spider scuddling back and forth between the ears.

Anyway..What I need don't have to be near perfect, just close enough to present a somewhat realistic conversion. Both examples are for me extremely usefull.

Best regards
Peter

Have a happy easter :wink:
God påske
Boa Páscoa
A' Chàisg sona
Христос воскресе
Joyeuses Pâques
¡Felices Pascuas!