Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

pixel to cm and cm to pixel how?

Posted on 2002-07-10
8
Medium Priority
?
6,774 Views
Last Modified: 2007-12-19
Hi ,

  How can i convert pixel value to cm and cm to pixel ?

Thanks..
0
Comment
Question by:Mozzak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7143128
Here's a simple example (copy/paste & place 2 buttons on the form...):

procedure CmPerPixel(canvas:TCanvas; var xcmpp,ycmpp: Real);
var h:HDC;
    hres,vres,hsiz,vsiz:integer;
begin
   h:=canvas.handle;
   hres := GetDeviceCaps(h,HORZRES);   {display width in pixels}
   vres := GetDeviceCaps(h,VERTRES);   {display height in pixels}
   hsiz := GetDeviceCaps(h,HORZSIZE);  {display width in mm}
   vsiz := GetDeviceCaps(h,VERTSIZE);  {display height in mm}
   xcmpp := hsiz/hres*0.1;
   ycmpp := vsiz/vres*0.1;
end;


procedure TForm1.Button1Click(Sender: TObject);
var xcmpp,ycmpp: Real; // pixel-> cm
begin
   CmPerPixel(Form1.Canvas, xcmpp,ycmpp);
   Caption:='100 pixels are '+FloatToStr(xcmpp*100)+' cm wide and '+FloatToStr(ycmpp*100)+' cm high';
end;

procedure TForm1.Button2Click(Sender: TObject);
var xcmpp,ycmpp: Real; // cm -> pixel
begin
   CmPerPixel(Form1.Canvas, xcmpp,ycmpp);
   Caption:='3 cm are '+FloatToStr(1/xcmpp*3)+' pixels wide and '+FloatToStr(1/ycmpp*3)+' pixels high';
end;

0
 
LVL 2

Accepted Solution

by:
bugroger earned 150 total points
ID: 7143133
hi,

try this function:

function GetPixelCM(pixelcount : integer) : extended;
begin
 // 1 inch = 2,54 cm
 Result := (2.54 / Screen.PixelsPerInch) * pixelCount;
end;

GL
 bug




0
 
LVL 2

Expert Comment

by:bugroger
ID: 7143134
i was too slow :-(
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:lopem
ID: 7143810
listening...
0
 

Author Comment

by:Mozzak
ID: 7145568
Bugroger i try to use your code because its easy..

Here is my code ;

MyPrinter = class
  function PixelToCM(pixelcount : integer):extended ;
  function CMToPixel(cm : extended):extended ;
end ;

function MyPrinter.CMToPixel(cm: extended):extended;
begin
  { i wrote }
  Result := (cm / 2.54) * Screen.PixelsPerInch ;
end;

function MyPrinter.PixelToCM(pixelcount: integer):extended;
begin
  { inch = 2,54 cm bugroger wrote }
  Result := (2.54 / Screen.PixelsPerInch) * pixelCount ;
end;
---------------------------------------------------------
when i try to use like these ;

procedure TForm1.Button1Click(Sender: TObject);
var
  vconv : TTinaPrinter ;
begin
  vconv := TTinaPrinter.Create ;
  Edit1.Text := Floattostr(vconv.PixelToCM(strtoint(Edit1.Text))) ;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  vconv : TTinaPrinter ;
begin
  vconv := TTinaPrinter.Create ;
  Edit1.Text := Floattostr(vconv.CMToPixel(strtofloat(Edit1.Text))) ;

end;
---------------------------------------------------------
problem occurs some times..for example try to calculate 5 first with button1 then use button2 for turn to 5 again but it will be 5,00000000000001.

  Can you give me the real value of inch/cm ? or different ssolution .
0
 

Author Comment

by:Mozzak
ID: 7145656
I had solve the problem and the real rate must be 2.5400181214134702506795530051344 .

Best Regards..
0
 

Author Comment

by:Mozzak
ID: 7145852
Still its not working after more than 2-3 try . For example 789 . What is the problem ? I'm taking 789,000000000002
0
 
LVL 2

Expert Comment

by:bugroger
ID: 7146806
hi Mozzak

this occurs because of rounding in FloatToStr
if you convert the extended value
to string and back.

now it should work! I have used FloatToStrF instead
of FloatToStr

Have a look to Button3Click.
If you don't convert the extended value (test)
to string and back it works fine!


procedure TForm1.Button1Click(Sender: TObject);
var
 vconv : MyPrinter;
begin
 vconv := MyPrinter.Create ;
 Edit1.Text := FloatToStrF(vconv.PixelToCM(strtoInt(Edit1.Text)), ffGeneral, 20, 0) ;
 vconv.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 vconv : MyPrinter;
begin
 vconv := MyPrinter.Create ;
 Edit1.Text := FloattostrF(vconv.CMToPixel(strtofloat(Edit1.Text)), ffGeneral, 5,0) ;
 vconv.Free;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
 vconv : MyPrinter;
 test : extended;

begin
 vconv := MyPrinter.Create ;
 test := vconv.PixelToCM(strtoInt(Edit1.Text));
 Edit1.Text := Floattostr(vconv.CMToPixel(test));
 vconv.Free;
end;


Hope this solve your problems
GL
 bug
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question