public static short HORZSIZE = 4; // Horizontal size in millimeters
public static short VERTSIZE = 6; // Vertical size in millimeters
public static short HORZRES = 8; // Horizontal width in pixels
public static short VERTRES = 10; // Vertical height in pixels
public static short PHYSICALOFFSETX = 112; // Physical Printable Area x
public static short PHYSICALOFFSETY = 113; // Physical Printable Area y
/// <summary>
/// Call the GetDeviceCaps Win32 method
/// </summary>
[DllImport("gdi32.dll")]
public static extern Int16 GetDeviceCaps(
[In] [MarshalAs (UnmanagedType.U4)] int hDc,
[In] [MarshalAs (UnmanagedType.U2)] Int16 funct);
/// <summary>
/// Return the device 'hard' margins for the passed in
/// Device Context handle. Return data in 1/100ths inch
/// </summary>
/// <param name="hDc">Input handle</param>
/// <param name="left">output left margin in 1/100ths inch</param>
/// <param name="top">output top margin in 1/100ths inch</param>
/// <param name="right">output right margin in 1/100ths inch</param>
/// <param name="bottom">output bottom margin in 1/100ths inch</param>
public void GetHardMargins(int hDc, ref float left, ref float top,
ref float right, ref float bottom)
{
float offx = Convert.ToSingle(GetDeviceCaps(hDc, PHYSICALOFFSETX));
float offy = Convert.ToSingle(GetDeviceCaps(hDc, PHYSICALOFFSETY));;
float resx = Convert.ToSingle(GetDeviceCaps(hDc, HORZRES));
float resy = Convert.ToSingle(GetDeviceCaps(hDc, VERTRES));
float hsz = Convert.ToSingle(GetDeviceCaps(hDc, HORZSIZE))/25.4f; // screen
float vsz = Convert.ToSingle(GetDeviceCaps(hDc, VERTSIZE))/25.4f; // screen
float ppix = resx/hsz;
float ppiy = resy/vsz;
left = (offx/ppix) * 100.0f;
top = (offy/ppix) * 100.0f;
bottom = top + (vsz * 100.0f);
right = left + (hsz * 100.0f);
}