Results 1 to 10 of 10

Thread: how do i get DPI of system

  1. #1

    Thread Starter
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    how do i get DPI of system

    hey

    is there any way to determine DPI of system ?
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: how do i get DPI of system

    Hi,

    You can try something like this;



    vb Code:
    1. Public Shared Function GetResolution(ByVal visual As Visual) As Point
    2.        Dim dpi As New Point(120, 120)
    3.      
    4.        Dim source As PresentationSource = PresentationSource.FromVisual(visual)
    5.        If source Is Nothing Then
    6.            Return dpi
    7.        End If
    8.      
    9.        Dim target As CompositionTarget = source.CompositionTarget
    10.      
    11.        Dim m As Matrix = target.TransformToDevice
    12.        Dim t As New MatrixTransform(m)
    13.      
    14.       Dim pt1 As New Point(0, 0)
    15.        pt1 = t.Transform(pt1)
    16.      
    17.      Dim pt2 As New Point(96, 96)
    18.      pt2 = t.Transform(pt2)
    19.      
    20.      dpi.X = pt2.X - pt1.X
    21.        dpi.Y = pt2.Y - pt1.Y
    22.       Return dpi
    23.    End Function
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: how do i get DPI of system

    Try this:

    Code:
            Dim g As Graphics = Me.CreateGraphics
            MessageBox.Show(String.Format("{0}x{1}", g.DpiX.ToString(), g.DpiY.ToString()))
            g.Dispose()

  4. #4

    Thread Starter
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: how do i get DPI of system

    hey
    @Negative0 this doesnot work for me
    MessageBox.Show(String.Format("{0}x{1}", g.DpiX.ToString(), g.DpiY.ToString()))
    @sparrow1
    is there namespaces and refrences required as i am getting error in visual ??
    ByVal visual As Visual


    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.
    pls help
    Last edited by su ki; Apr 8th, 2009 at 07:06 AM.
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: how do i get DPI of system

    Yes,

    System.Windows
    System.Windows.Media

    Think thats all you need.

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: how do i get DPI of system

    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?

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: how do i get DPI of system

    Quote Originally Posted by Negative0 View Post
    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?
    Works for me too.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: how do i get DPI of system

    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_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
    or

    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_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
    or

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: how do i get DPI of system

    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()))

  10. #10

    Thread Starter
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: how do i get DPI of system

    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
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width