利用Win32API 将毫米转换成像素

/// <summary>

/// 以毫米为单位的显示宽度

/// <summary>

const int HORZSIZE = 4;

 

/// <summary>

/// 以像素为单位的显示宽度 0~65535

/// <summary>

const int HORZRES = 8;

const int LOGPIXELSX = 88;

const int LOGPIXELSY = 90;

 

[System.Runtime.InteropService.DllImport("gdi32.dll")]

private static extern int GetDeviceCaps(IntPtr hdc,int Index);

/// <summary>

/// 毫米转换成像素

/// <summary>

/// <param name="handle">handle</param>

/// <param name="length">毫米</param>

/// <returns></returns>

public static double MillimetersToPixelsWith(IntPtr handle,double length)

{

  System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(handle);

  IntPtr hdc = g.GetHdc();

  int width = GetDeviceCaps(hdc,HORZSIZE);

  int pixels = GetDeviceCaps(hdc,HORZRES);

  g.ReleaseHdc(Hdc);

  return (double)pixels / (double)width * (double)length;

}

 

posted @ 2020-12-14 09:40  枫卍  阅读(154)  评论(0编辑  收藏  举报