Converting metric units into pixels????????
Converting metric units into pixels????????
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
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
when I do bad, I feel bad,
and that is my religion.
Abraham Lincoln
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
"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.
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.
Hehe.. Thank you for your reply
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.

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
when I do bad, I feel bad,
and that is my religion.
Abraham Lincoln
- Arctic Fox
- Enthusiast
- Posts: 609
- Joined: Sun Dec 21, 2008 5:02 pm
- Location: Aarhus, Denmark
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
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
Something like this maybe:
Debug CmToPixHorz (37.5)
Debug CmToPixVert (30.0)
My screen is 1280x1024 and 37.5x30 cm
I would say it's a match.
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 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.
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
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
when I do bad, I feel bad,
and that is my religion.
Abraham Lincoln
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.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.
With the code I posted I get 1280.
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
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
God påske
Boa Páscoa
A' Chàisg sona
Христос воскресе
Joyeuses Pâques
¡Felices Pascuas!
seen my calculations.Arctic Fox and Luis's examples gives far better results.
Mine calculations weren't even close

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

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
when I do bad, I feel bad,
and that is my religion.
Abraham Lincoln