Click here to Skip to main content
6,292,811 members and growing! (10,351 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate

Capturing the Screen Image in C#

By Agha Ali Raza

An article on capturing the Screen using Win32 APIs in C#
C#, Windows, .NET 1.0, Dev
Posted:11 Oct 2002
Updated:27 Feb 2003
Views:143,205
Bookmarked:57 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
31 votes for this article.
Popularity: 5.91 Rating: 3.96 out of 5
4 votes, 14.3%
1
1 vote, 3.6%
2
2 votes, 7.1%
3
10 votes, 35.7%
4
11 votes, 39.3%
5

Introduction

I am working on a desktop sharing type application in C#. First problem that I encountered was that there was not much in C# to capture the desktop image. After a bit of research I came to know that I shall have to use the Win32 APIs to provide this functionality to my application. I made following three classes to perform this job.

  1. PlatformInvokeGDI32: All the GDI32.dll APIs being used in this application are placed in this class.
  2. PlatformInvokeUSER32: All the User32.dll APIs have been placed in this class.
  3. CaptureScreen: In this class I have provided a simple static function GetDesktopImage that captures the screen image using the APIs  given in PlatformInvokeGDI32 and PlatformInvokeUSER32 and returns it as a bitmap.

You can very easily place these classes in your C# application. Just copy and paste following code in your C# project without changing any thing at all. If you want to use accompanying source files, you can  add the CaptureScreen namespace in your project or just change the CaptureScreen namespace in these classes with your project namespace and that's all.

The beautiful source code

/// <summary>

/// This class shall keep the GDI32 APIs used in our program.

/// </summary>

public class PlatformInvokeGDI32
{
#region Class Variables
    public const int SRCCOPY = 13369376;
#endregion
#region Class Functions<br>
    [DllImport("gdi32.dll",EntryPoint="DeleteDC")]
    public static extern IntPtr DeleteDC(IntPtr hDc);

    [DllImport("gdi32.dll",EntryPoint="DeleteObject")]
    public static extern IntPtr DeleteObject(IntPtr hDc);

    [DllImport("gdi32.dll",EntryPoint="BitBlt")]
    public static extern bool BitBlt(IntPtr hdcDest,int xDest,
        int yDest,int wDest,int hDest,IntPtr hdcSource,
        int xSrc,int ySrc,int RasterOp);

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
        int nWidth, int nHeight);

    [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport ("gdi32.dll",EntryPoint="SelectObject")]
    public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);
#endregion

#region Public Constructor
}

/// <summary>

/// This class shall keep the User32 APIs used in our program.

/// </summary>

public class PlatformInvokeUSER32
{
#region Class Variables
    public const int SM_CXSCREEN=0;
    public const int SM_CYSCREEN=1;
#endregion 

#region Class Functions
    [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();

    [DllImport("user32.dll",EntryPoint="GetDC")]
    public static extern IntPtr GetDC(IntPtr ptr);

    [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
    public static extern int GetSystemMetrics(int abc);

    [DllImport("user32.dll",EntryPoint="GetWindowDC")]
    public static extern IntPtr GetWindowDC(Int32 ptr);

    [DllImport("user32.dll",EntryPoint="ReleaseDC")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);

#endregion
}

/// <summary>

/// This class shall keep all the functionality 

/// for capturing the desktop.

/// </summary>

public class CaptureScreen
{
#region Class Variable Declaration
    protected static IntPtr m_HBitmap;
#endregion

///

/// This class shall keep all the functionality for capturing

/// the desktop.

///

public class CaptureScreen
{
  #region Public Class Functions
  public static Bitmap GetDesktopImage()
  {
    //In size variable we shall keep the size of the screen.

    SIZE size;

    //Variable to keep the handle to bitmap.

    IntPtr hBitmap;

    //Here we get the handle to the desktop device context.

    IntPtr  hDC = PlatformInvokeUSER32.GetDC
                  (PlatformInvokeUSER32.GetDesktopWindow());

    //Here we make a compatible device context in memory for screen

    //device context.

    IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

    //We pass SM_CXSCREEN constant to GetSystemMetrics to get the

    //X coordinates of the screen.

    size.cx = PlatformInvokeUSER32.GetSystemMetrics
              (PlatformInvokeUSER32.SM_CXSCREEN);

    //We pass SM_CYSCREEN constant to GetSystemMetrics to get the

    //Y coordinates of the screen.

    size.cy = PlatformInvokeUSER32.GetSystemMetrics
              (PlatformInvokeUSER32.SM_CYSCREEN);

    //We create a compatible bitmap of the screen size and using

    //the screen device context.

    hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap
                (hDC, size.cx, size.cy);

    //As hBitmap is IntPtr, we cannot check it against null.

    //For this purpose, IntPtr.Zero is used.

    if (hBitmap!=IntPtr.Zero)
    {
      //Here we select the compatible bitmap in the memeory device

      //context and keep the refrence to the old bitmap.

      IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject
                             (hMemDC, hBitmap);
      //We copy the Bitmap to the memory device context.

      PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC,
                                 0, 0,PlatformInvokeGDI32.SRCCOPY);
      //We select the old bitmap back to the memory device context.

      PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
      //We delete the memory device context.

      PlatformInvokeGDI32.DeleteDC(hMemDC);
      //We release the screen device context.

      PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.
                                     GetDesktopWindow(), hDC);
      //Image is created by Image bitmap handle and stored in

      //local variable.

      Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap); 
      //Release the memory to avoid memory leaks.

      PlatformInvokeGDI32.DeleteObject(hBitmap);
      //This statement runs the garbage collector manually.

      GC.Collect();
      //Return the bitmap 

      return bmp;
    }
    //If hBitmap is null, retun null.

    return null;
  }
  #endregion
}

//This structure shall be used to keep the size of the screen.

public struct SIZE
{
    public int cx;
    public int cy;
}

Conclusion

The demo application with this code shows the use of these classes. Its a very simple windows application in which there is a simple form having a menu and a picture box control. Capture Screen menu item of menu is used to capture the screen and assign to the image property of picture box control. I hope you like this code. I have commented each line of this code to make it self explanatory. If there is still something confusing for you, please let me know. Good Luck!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Agha Ali Raza


Member

Occupation: Web Developer
Location: Pakistan Pakistan

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 40 (Total in Forum: 40) (Refresh)FirstPrevNext
GeneralMouse Position PinmembermicoxXx8:44 3 Aug '08  
GeneralRe: Mouse Position Pinmembercheng_j_zhang18:04 11 Nov '08  
GeneralRe: Mouse Position Pinmembercheng_j_zhang18:07 11 Nov '08  
GeneralDoesn't work when computer is locked PinmemberJDBP5:57 4 Apr '08  
Generalmemory leak Pinmemberrattle5jonq8:48 8 Nov '07  
GeneralDoing it with C# itself is much simpler... Pinmemberdalek99:06 30 Nov '06  
GeneralRe: Doing it with C# itself is much simpler... PinmemberEl Chubb17:40 11 Jul '08  
GeneralRe: Doing it with C# itself is much simpler... PinmembermicoxXx8:42 3 Aug '08  
GeneralCopyFromScreen Pinmembers.struck11:37 9 Sep '08  
QuestionVideo PinmemberRichardUci6:12 7 Nov '06  
GeneralDoesn't capture Layered (trasnparent, alpha blended) windows PinmemberJaykul9:14 9 Aug '05  
GeneralRe: Doesn't capture Layered (trasnparent, alpha blended) windows PinmemberJonFrost21:09 19 Feb '07  
GeneralNULL retured if I call the function 42 times Pinmemberraed4:10 23 Jun '05  
GeneralRe: NULL retured if I call the function 42 times PinmemberJaykul9:36 9 Aug '05  
GeneralMajor Memory Leak PinmemberFalazar10:58 6 May '05  
GeneralRe: Major Memory Leak PinmemberJisu7413:51 11 Nov '05  
GeneralRe: Major Memory Leak Pinmemberpsyphen4:46 21 May '09  
Generalthe Dll functions Pinmembertokyo_bme8:36 2 Mar '05  
GeneralRe: the Dll functions Pinmemberjjreilly9:18 2 Mar '05  
GeneralRe: the Dll functions Pinmembertokyo_bme21:30 4 Mar '05  
Generalscreen capture in web app Pinmembereheuer3:34 17 Feb '05  
GeneralRe: screen capture in web app PinmemberTimmy2322:25 7 Mar '05  
GeneralBug with multiple monitors PinsussSherrod Segraves6:52 24 Sep '04  
GeneralBlack screen???Help!!!!!!! PinmemberWalkWorld20:05 5 Sep '04  
GeneralOverlay? PinmemberJakub Florczyk23:59 22 Jul '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Feb 2003
Editor: Chris Maunder
Copyright 2002 by Agha Ali Raza
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project