Click to See Complete Forum and Search --> : Pixels to Meters Conversion


prabhakar157
June 17th, 2006, 02:28 AM
Using the Mapping modes like MM_LOENGLISH etc change the client view coordinate system to inches... but

How to get the units in Meters? I mean.... provided ne amount of pixels units i want it to be displayed in Meters...

I made use of thid code which is actually a Delphi program
I modified it to MFC code..... (and this is an SDI Application)


class CDoublePoint : CObject
{
public:
CDoublePoint();
virtual ~CDoublePoint()
{
}
CDoublePoint(double X,double Y); //Parameter Constructor
double PixelHorizToMM(int PixelX);
double PixelVertToMM(int PixelY);
CDoublePoint(const CDoublePoint& D_PointSrc);
void PixelPointToMM(CPoint PixelPt);

public:
double D_x,D_y;
};




CDoublePoint::CDoublePoint()
{
}
CDoublePoint::CDoublePoint(const CDoublePoint& D_PointSrc) //Copy Constructor
{
this->D_x = D_PointSrc.D_x;
this->D_y = D_PointSrc.D_y;
}
CDoublePoint::CDoublePoint(double X,double Y) //Parameter Constructor
{
this->D_x = X;
this->D_y = Y;
}
double CDoublePoint::PixelHorizToMM(int PixelX)
{
HDC hdcDesk;
hdcDesk = GetDC(AfxGetMainWnd()->GetSafeHwnd());
//GetDC(0) was used in Delphi

this->D_x = PixelX * (GetDeviceCaps(hdcDesk, HORZSIZE) /
GetDeviceCaps(hdcDesk, HORZRES));
ReleaseDC(AfxGetMainWnd()->GetSafeHwnd(),hdcDesk);

// and ReleaseDC(0,hdcDesk) was used here
return this->D_x;
}

double CDoublePoint::PixelVertToMM(int PixelY)
{
HDC hdcDesk;
hdcDesk = GetDC(AfxGetMainWnd()->GetSafeHwnd());

this->D_y = PixelY * (GetDeviceCaps(hdcDesk, VERTSIZE) /
GetDeviceCaps(hdcDesk, VERTRES));
ReleaseDC(AfxGetMainWnd()->GetSafeHwnd(),hdcDesk);

return this->D_y;
}

void CDoublePoint::PixelPointToMM(CPoint PixelPt)
{
this->D_x = PixelHorizToMM(PixelPt.x);
this->D_y = PixelVertToMM(PixelPt.y);

}


I tried to displayed the values on the screen

//inside OnDraw method

...
....
....

PixelinMM.PixelPointToMM(CPoint(10,10));
temp_String.Format("Value of 10 Pixel in MM for Horizontal Resolution: %g",PixelinMM.D_x);
pDC->TextOut(400,400,temp_String);

temp_String.Format("Value of 10 Pixel in MM for Vertical Resolution : %g",PixelinMM.D_y);
pDC->TextOut(400,430,temp_String);
}


May be...... i didn't get the proper handle for the window inspite of using AfxGetMainWnd().....
Moreover this code seems complex to me......

How do i make it a simple one?

VladimirF
June 21st, 2006, 07:19 PM
Using the Mapping modes like MM_LOENGLISH etc change the client view coordinate system to inches... but

How to get the units in Meters? I mean.... provided ne amount of pixels units i want it to be displayed in Meters...
Couldn't you just use MM_LOMETRIC?

olivthill
June 22nd, 2006, 05:20 AM
The measures returned by GetDeviceCaps() are not reliable.
There is no way to have the correct sizes on all systems. Sometimes, the figures are correct. Sometimes, they are more or less different than those measured in reality.
See http://www.codeguru.com/forum/showthread.php?t=348483&highlight=GetDeviceCaps