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

Just starting out? Need help? Post your questions and find answers here.
Peter_DK
User
User
Posts: 20
Joined: Fri Feb 17, 2006 5:09 pm
Location: Denmark

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

Post 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
When I do good, I feel good;
when I do bad, I feel bad,
and that is my religion.

Abraham Lincoln
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
Peter_DK
User
User
Posts: 20
Joined: Fri Feb 17, 2006 5:09 pm
Location: Denmark

Post 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.
When I do good, I feel good;
when I do bad, I feel bad,
and that is my religion.

Abraham Lincoln
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Post 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
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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.
Last edited by luis on Mon Apr 06, 2009 7:42 pm, edited 1 time in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :)
I may look like a mule, but I'm not a complete ass.
Peter_DK
User
User
Posts: 20
Joined: Fri Feb 17, 2006 5:09 pm
Location: Denmark

Post 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
When I do good, I feel good;
when I do bad, I feel bad,
and that is my religion.

Abraham Lincoln
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Peter_DK
User
User
Posts: 20
Joined: Fri Feb 17, 2006 5:09 pm
Location: Denmark

Post 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!
When I do good, I feel good;
when I do bad, I feel bad,
and that is my religion.

Abraham Lincoln
Post Reply