Determine system's dpi setting

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can somebody please tell me how to determine the system's dpi setting (e.g.,
96 dpi for normal size, 120 dpi for large size or some other value for custom
dpi).

Thanks,
Lance
 
This is one way:

<DllImport("user32.dll")> _
Public Shared Function GetDC(ByVal hWnd As HandleRef) As IntPtr
End Function

<DllImport("gdi32.dll")> _
Public Shared Function GetDeviceCaps(ByVal hDC As IntPtr, ByVal nIndex As
Integer) As Integer
End Function

<DllImport("user32.dll")> _
Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr)
As Integer
End Function

dDC = GetDC(Nothing) ' Get desktop DC
dpi = GetDeviceCaps(dDC, 88)
MsgBox("DPI Value: " & dpi.ToString())
rv = ReleaseDC(Nothing, dDC)

Alternatively,

Dim dDC As IntPtr
Dim rv As Integer
Dim g As Graphics

dDC = GetDC(Nothing) 'Get desktop DC
g = Graphics.FromHdc(dDC)
MsgBox("DPI Value: " & g.DpiX.ToString())
g.Dispose()
rv = ReleaseDC(Nothing, dDC)

Yet another way, if you have a Windows form (Form1),

Dim g As Graphics

g = Graphics.FromHwnd (Form1.Handle)
MsgBox("DPI Value: " & g.DpiX.ToString())
g.Dispose()


Hope this helps?

Can somebody please tell me how to determine the system's dpi setting (e.g.,
96 dpi for normal size, 120 dpi for large size or some other value for
custom
dpi).

Thanks,
Lance
 
ljlevend2 said:
Can somebody please tell me how to determine the system's dpi setting
(e.g.,
96 dpi for normal size, 120 dpi for large size or some other value for
custom
dpi).

Create a 'Graphics' object for a form, for example, and check its 'DpiX' and
'DpiY' properties.
 
Back
Top