Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / May 2007

Tip: Looking for answers? Try searching our database.

How do I get the DPI of the primary screen?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Thielen - 30 Apr 2007 20:52 GMT
I've looked and looked and can't find anything in the .NET API. But it must
be there...

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Jeffrey Tan[MSFT] - 01 May 2007 03:54 GMT
Hi dave,

In Win32 GDI, the DPI capability of a device is obtained from GetDeviceCaps
with LOGPIXELSX and LOGPIXELSY. Based on my research, there are some .Net
BCL internal classes calling GetDeviceCaps to obtain the DPI, such as
System.Windows.Forms.Internal.DeviceContext.DPI property. However, they are
not public available. So I would recommend you to p/invoke GetDeviceCaps
with LOGPIXELSX and LOGPIXELSY to get this done. Note: to obtain the
primary screen's capability, you should pass NULL as the first "hdc"
parameter to GetDeviceCaps.

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 01 May 2007 06:07 GMT
Wow - you're kidding me. THey don't have that in the .NET API yet? No wonder
I couldn't find it. Ok, I'll p/invoke.

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

> Hi dave,
>
[quoted text clipped - 30 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 01 May 2007 06:31 GMT
Hi;

I've done the following:

        [DllImport("gdi32.dll")]
        private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
        private const int LOGPIXELSX = 88;
...
_dpi = GetDeviceCaps(IntPtr.Zero, LOGPIXELSX);

But it is returning 0 - any idea why?

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

> Hi dave,
>
[quoted text clipped - 30 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeffrey Tan[MSFT] - 01 May 2007 08:33 GMT
Oh, my mistake, we should not pass Null for the first parameter, it is an
invalid handle for GetDeviceCaps. We should first create a screen DC with
CreateDC and then pass this screen DC to the GetDeviceCaps. Below code
snippet works well on my side:

[DllImport("gdi32.dll", EntryPoint = "CreateDC", CharSet = CharSet.Auto,
SetLastError = true)]
private static extern IntPtr CreateDC(string lpszDriver, string
lpszDeviceName, string lpszOutput, IntPtr devMode);

[DllImport("gdi32.dll", SetLastError=true)]
private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
private const int LOGPIXELSX = 88;

IntPtr hdc = CreateDC("DISPLAY", null, null, IntPtr.Zero);
if (hdc == IntPtr.Zero)
{
   MessageBox.Show(Marshal.GetLastWin32Error().ToString());
   return;
}
int _dpi = GetDeviceCaps(hdc, LOGPIXELSX);
if (_dpi == 0)
{
   MessageBox.Show(Marshal.GetLastWin32Error().ToString());
   return;
}

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 01 May 2007 17:30 GMT
I remember it being null from my C++ days too - I guess we both have the
wrong memory. I added a DeleteDC and posted the code at
http://www.davidthielen.info/programming/2007/05/get_screen_dpi_.html for the
next person trying to find this.

Thank you for your help.

Signature

thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

> Oh, my mistake, we should not pass Null for the first parameter, it is an
> invalid handle for GetDeviceCaps. We should first create a screen DC with
[quoted text clipped - 46 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeffrey Tan[MSFT] - 02 May 2007 04:43 GMT
Hi dave,

Thank you for sharing the result on your page!

Ok, if you need further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.