How To Get The Display’s Color Depth using NetCF Version 1

I've seen this asked about recently on the NetCF (microsoft.public.dotnet.framework.compactframework) newsgroup, and it fits nicely with yesterday's display related p/invoke, too... 🙂

You may have noticed that, in version 1 of the .NET Compact Framework, the Screen object (System.Windows.Forms.Screen) does not specify the color depth of the display device (Bounds, PrimaryScreen, WorkingArea are provided).  The color depth may be useful, if you are writing an application to working with graphics, you may wish to limit a user's color palette based on what the display is capable of showing, for example.

Fortunately, getting this information is not difficult.  You only need two p/invoke calls: GetDC() and GetDeviceCaps().

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class DisplayInfo
    public static void Main()
        // get the dimensions of the display
        Rectangle screen = Screen.PrimaryScreen.Bounds;

        // get the color depth (bits per pixel)
        //  first, we get the handle to the primary display (using GetDC)
        IntPtr deviceHandle = GetDC(IntPtr.Zero);
        if(IntPtr.Zero == deviceHandle)
            // our call to GetDC failed
            MessageBox.Show("Failed to get device handle");
        // if we get here, we can query the device capabilities
        Int32 colorDepth = GetDeviceCaps(deviceHandle, BitsPerPixel);       

        // release the device context
        //  0 == not released
        //  1 == released
        if(0 == ReleaseDC(IntPtr.Zero, deviceHandle))
            MessageBox.Show("Failed to release the device handle");
        // build a string containing the information we will show
        String msg = String.Format("{0} x {1} {2} bpp",
        // display the screen info
        MessageBox.Show(msg, "Screen information");

    // GetDeviceCaps index value(s)
    private const Int32 BitsPerPixel = 12;

    // pinvoke methods
    [DllImport("coredll.dll", SetLastError=true)]
    private extern static IntPtr GetDC(IntPtr hwnd);
    [DllImport("coredll.dll", SetLastError=true)]
    private extern static Int32 ReleaseDC(IntPtr hwnd,
                                        IntPtr hdc);

    [DllImport("coredll.dll", SetLastError=true)]
    private extern static Int32 GetDeviceCaps(IntPtr hdc,
                                            Int32 index);

-- DK

[Edit: Add call to ReleaseDC]

This posting is provided "AS IS" with no warranties, and confers no rights.

Comments (5)
  1. Adam says:

    I wonder…does this mean that .NET CF is *overly* compact e.g. that you went too far.

    In particular, didn’t we (MSFT) tell people that they could write their app in .NET and yet if they’re escaping out often — that seems to break the claim/promise.

  2. David Kline says:


    The full .NET Framework also does not provide color depth in v1.x ( In this instance, the limitation is not due to NetCF being overly agressive in our factorization.


  3. SteveP says:

    Just thought I’d point out that there’s no ReleaseDC in the code sample. I think it should come after the GetDeviceCaps line, otherwise you’ll get a memory leak.



  4. David Kline says:


    Great catch! I’ll update the snippet.



Comments are closed.

Skip to main content