hey
is there any way to determine DPI of system ?
Printable View
hey
is there any way to determine DPI of system ?
Hi,
You can try something like this;
vb Code:
Public Shared Function GetResolution(ByVal visual As Visual) As Point Dim dpi As New Point(120, 120) Dim source As PresentationSource = PresentationSource.FromVisual(visual) If source Is Nothing Then Return dpi End If Dim target As CompositionTarget = source.CompositionTarget Dim m As Matrix = target.TransformToDevice Dim t As New MatrixTransform(m) Dim pt1 As New Point(0, 0) pt1 = t.Transform(pt1) Dim pt2 As New Point(96, 96) pt2 = t.Transform(pt2) dpi.X = pt2.X - pt1.X dpi.Y = pt2.Y - pt1.Y Return dpi End Function
Try this:
Code:Dim g As Graphics = Me.CreateGraphics
MessageBox.Show(String.Format("{0}x{1}", g.DpiX.ToString(), g.DpiY.ToString()))
g.Dispose()
hey
@Negative0 this doesnot work for me
@sparrow1Quote:
MessageBox.Show(String.Format("{0}x{1}", g.DpiX.ToString(), g.DpiY.ToString()))
is there namespaces and refrences required as i am getting error in visual ??
Quote:
ByVal visual As Visual
pls helpQuote:
Type 'Visual' is not defined.
Type 'PresentationSource' is not defined.
Type 'CompositionTarget' is not defined.
Type 'Matrix' is not defined.
Type 'MatrixTransform' is not defined.
Yes,
System.Windows
System.Windows.Media
Think thats all you need.
Can you explain what doesn't work? It worked perfectly on my machine, returning 96x96. What did it return on your machine? What are you expecting it to return?
orCode:Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
Public Class MyWMIQuery
Public Overloads Shared Function Main() As Integer
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_DisplayConfiguration")
For Each queryObj As ManagementObject in searcher.Get()
Console.WriteLine("-----------------------------------")
Console.WriteLine("Win32_DisplayConfiguration instance")
Console.WriteLine("-----------------------------------")
Console.WriteLine("LogPixels: {0}", queryObj("LogPixels"))
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Function
End Class
End Namespace
orCode:Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
Public Class MyWMIQuery
Public Overloads Shared Function Main() As Integer
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_DisplayControllerConfiguration")
For Each queryObj As ManagementObject in searcher.Get()
Console.WriteLine("-----------------------------------")
Console.WriteLine("Win32_DisplayControllerConfiguration instance")
Console.WriteLine("-----------------------------------")
Console.WriteLine("HorizontalResolution: {0}", queryObj("HorizontalResolution"))
Console.WriteLine("VerticalResolution: {0}", queryObj("VerticalResolution"))
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Function
End Class
End Namespace
Code:Imports System
Imports System.Management
Imports System.Windows.Forms
Namespace WMISample
Public Class MyWMIQuery
Public Overloads Shared Function Main() As Integer
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", _
"SELECT * FROM Win32_VideoController")
For Each queryObj As ManagementObject in searcher.Get()
Console.WriteLine("-----------------------------------")
Console.WriteLine("Win32_VideoController instance")
Console.WriteLine("-----------------------------------")
Console.WriteLine("CurrentHorizontalResolution: {0}", queryObj("CurrentHorizontalResolution"))
Console.WriteLine("CurrentVerticalResolution: {0}", queryObj("CurrentVerticalResolution"))
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try
End Function
End Class
End Namespace
Well resolution is not the same as DPI. DPI is a setting you can change to make the fonts larger without changing your resolution.
If you just want the resolution, there is no need for WMI, you can use the My.Computer.Screen objecT:
Code:MessageBox.Show(String.Format("{0}x{1}", My.Computer.Screen.Bounds.Width.ToString(), My.Computer.Screen.Bounds.Height.ToString()))
hey
*@ Pino thanks for telling about required namespaces.
@Negative0 , it shows 96 when dpi setting is 96 but it doesnot change when i switch it to 120 which it suppose to be.
actually i am checking it on resolution 1920*1200 and dpi 96 or 120
i understand that resolution is not same as dpi
@dbasnett , i hv not checked ur solution as i was not on my pc , let u know soon
su ki