最近一直在烦恼,到底怎样,怎样获取物理尺寸、物理像素之间的对应关系呢?
最常见的莫过于以下的写法:
// 计算一个设备单位等于多少0.1mm
double scaleX = 254.0 / (double)GetDeviceCaps(dc.m_hAttribDC,LOGPIXELSX); // 2048X1152 96DPI
double scaleY = 254.0 / (double)GetDeviceCaps(dc.m_hAttribDC, LOGPIXELSY); // 2048X1152 96DPI
我自己是这样写的:
double mmScreenX = (double)::GetDeviceCaps(dc.m_hAttribDC, HORZSIZE); // 722mm
double nMMPixelX = (double)::GetDeviceCaps(dc.m_hAttribDC, HORZRES)/mmScreenX;
double mmScreenY = (double)::GetDeviceCaps(dc.m_hAttribDC, VERTSIZE); // 406mm
double nMMPixelY = (double)::GetDeviceCaps(dc.m_hAttribDC, VERTRES)/mmScreenY;
用VS2010调试的结果:
scaleX = scaleY = 2.6458333333333335 // 每像素对应多少0.1mm
nMMPixelX = 2.8365650969529086 // 每毫米对应多少像素
nMMPixelY = 2.8374384236453203 // 每毫米对应多少像素
哎,254.0那个写法是最流行的,但最流行的问题:为什么HORZSIZE的实际值不等于25.4*HORZRES/LOGPIXELSX?
有个兄弟说的特别清楚,这些东西不是绝对的正确,依赖系统字体、Windows版本,我其实被微软这些操蛋的东西搞晕了,写这里继续操蛋,哎。
记住:LOGPIXELSX的值一般是固定的(96DPI,120DPI,听说和字体有关),另外两个成比例同时放大缩小。
关于GDI不想再说了,过时的东西,现在流行的不再是逻辑坐标、视口坐标、设备坐标如何转换了。看看GDI+,现在流行的是Matrix:矩阵革命(黑客帝国啊)。
有了Matrix那可是利器啊,你想干啥,就使用Matrix吧。放大、缩小、平移、反转、扭曲,一切都变得简单。熟悉Android的都知道Matrix的强大,可以save,然后resotre。当然这里也可以。
譬如,你坐标系是Y轴是反的,标注是正的。http://www.bobpowell.net/ReverseAxisText.htm
摘录一段老外写的:http://www.bobpowell.net/mappingmodes.aspx
GDI+ has three distinct graphical spaces. They are the World, Page and Device spaces. The world space corresponds to the GDI logical space but with one important difference, the coordinate system is based on floating point rather than integer units. This means that real-world measurements can be catered for without the potential for overflowing the logical coordinate space. You can for example specify a measurement of 1.75 inches or 300,000.5 millimeters. The Page space controls the relationship between the abstract world space and the Device space and ensures that pixels placed an inch apart in the world space appear an inch apart on any and all devices. You can specify that the page units are measured in inches, millimeters, pixels and points which are all real-world measurements and an additional couple of pseudo real-world standards called Device and Display which are 1/300th of an inch and 1/75th of an inch respectively.When a pixel is plotted in the world space, it is automatically transformed by the page space to the device space and displayed in the correct position on screen. This type of bucket-brigade calculation system is called a pipeline and may have other steps inserted into the chain to further modify the appearance of the image rendered on the display. The important one from the point of view of this discussion is the current graphics transform which is stored in a Matrix which is a property of the Graphics object used to do drawing in GDI+.