像素转换mm

public static double MillimetersToPixelsWidth(double length) //length是毫米,1厘米=10毫米
{
System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(p.Handle);
TextBox txt = new TextBox();
IntPtr hdc = g.GetHdc();
int width = GetDeviceCaps(hdc, 4); // HORZRES
int pixels = GetDeviceCaps(hdc, 8); // BITSPIXEL
g.ReleaseHdc(hdc);
return (((double)pixels / (double)width) * (double)length);
}
[DllImport("gdi32.dll")]
private static extern int GetDeviceCaps(IntPtr hdc, int Index);

posted @ 2016-11-20 09:31  陈夏松  阅读(329)  评论(0编辑  收藏  举报