using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace XCommon.Image
{
///
/// 像素与物理长度的计算操作类
///
public class PixelsCalculationHelper
{
#region Win32 API
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr ptr);
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
int nIndex // index of capability
);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
#endregion
#region DeviceCaps常量
const int HORZRES = 8;//物理屏幕宽度mm
const int VERTRES = 10;//物理屏幕高度mm
const int LOGPIXELSX = 88;//逻辑像素x
const int LOGPIXELSY = 90;//逻辑像素y
const int DESKTOPVERTRES = 117;//桌面高度
const int DESKTOPHORZRES = 118;//桌面宽度
#endregion
#region 属性
///
/// 宽度转化 length是毫米,1厘米=10毫米
///
///
///
public static double MillimetersToPixelsWidth(double length)
{
System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
IntPtr hdc = g.GetHdc();
int width = GetDeviceCaps(hdc, 4); // HORZRES
int pixels = GetDeviceCaps(hdc, 8); // BITSPIXEL
double fg = pixels / width;
g.ReleaseHdc(hdc);
return (((double)pixels / (double)width) * (double)length);
}
///
/// 高度转化 length是毫米,1厘米=10毫米
///
///
///
public static double MillimetersToPixelsHeight(double length)
{
System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
IntPtr hdc = g.GetHdc();
int height = GetDeviceCaps(hdc, 6); // HORZRES
int pixels = GetDeviceCaps(hdc, 10); // BITSPIXEL
double fg = pixels / height;
g.ReleaseHdc(hdc);
return (((double)pixels / (double)height) * (double)length);
}
///
/// 获取屏幕分辨率当前物理大小
///
public static Size WorkingArea
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, HORZRES);
size.Height = GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
///
/// 当前系统DPI_X 大小 一般为96
///
public static int DpiX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
///
/// 当前系统DPI_Y 大小 一般为96
///
public static int DpiY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(IntPtr.Zero, hdc);
return DpiX;
}
}
///
/// 获取真实设置的桌面分辨率大小
///
public static Size DESKTOP
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
Size size = new Size();
size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return size;
}
}
///
/// 获取宽度缩放百分比
///
public static float ScaleX
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
int d = GetDeviceCaps(hdc, HORZRES);
float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
ReleaseDC(IntPtr.Zero, hdc);
return ScaleX;
}
}
///
/// 获取高度缩放百分比
///
public static float ScaleY
{
get
{
IntPtr hdc = GetDC(IntPtr.Zero);
float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
ReleaseDC(IntPtr.Zero, hdc);
return ScaleY;
}
}
#endregion
#region 1个像素代表的实际长度
///
/// 1个像素代表的实际长度
///
/// 像素大小
/// 每英寸点数
///
public static double PixelsToInches(int pixels, int dpi)
{
return (double)(pixels) / dpi * 25.4;
}
#endregion
}
}