Click here to Skip to main content
Click here to Skip to main content

Capturing the Screen Image in C#

, 27 Feb 2003
Rate this:
Please Sign up or sign in to vote.
An article on capturing the Screen using Win32 APIs in C#


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;
#region Class Functions<br>
    public static extern IntPtr DeleteDC(IntPtr hDc);

    public static extern IntPtr DeleteObject(IntPtr hDc);

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

#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;

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

    public static extern IntPtr GetDC(IntPtr ptr);

    public static extern int GetSystemMetrics(int abc);

    public static extern IntPtr GetWindowDC(Int32 ptr);

    public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);


/// <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;

/// 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

    //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. = PlatformInvokeUSER32.GetSystemMetrics

    //We pass SM_CYSCREEN constant to GetSystemMetrics to get the
    //Y coordinates of the screen. = PlatformInvokeUSER32.GetSystemMetrics

    //We create a compatible bitmap of the screen size and using
    //the screen device context.
    hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap

    //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,,, 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.
      //We release the screen device context.
                                     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.
      //This statement runs the garbage collector manually.
      //Return the bitmap 
      return bmp;
    //If hBitmap is null, retun null.
    return null;

//This structure shall be used to keep the size of the screen.
public struct SIZE
    public int cx;
    public int cy;


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!


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
Web Developer
Pakistan Pakistan
No Biography provided

Comments and Discussions

QuestionLicensing information thought PinmemberMember 1079076813-May-14 11:11 
GeneralMy vote of 5 Pinmemberemanzer013-Jun-12 18:17 
GeneralRemote System Screen Capture Pinmembersksaininet25-Dec-10 21:17 
Generalsimplier PinmemberLucian-aSterX15-Sep-10 2:19 
GeneralRe: simplier PinmemberHoria Tudosie15-Jun-12 11:19 
GeneralNot working in game PinmemberShargon_8515-Apr-10 23:59 
GeneralThank you PinmemberB.V.Papadopoulos4-Aug-09 22:16 
GeneralMouse Position PinmembermicoxXx3-Aug-08 8:44 
GeneralRe: Mouse Position Pinmembercheng_j_zhang11-Nov-08 18:04 
GeneralRe: Mouse Position Pinmembercheng_j_zhang11-Nov-08 18:07 
GeneralDoesn't work when computer is locked PinmemberJDBP4-Apr-08 5:57 
GeneralRe: Doesn't work when computer is locked [modified] Pinmemberromanof13-Feb-13 19:36 
Generalmemory leak Pinmemberrattle5jonq8-Nov-07 8:48 
GeneralDoing it with C# itself is much simpler... Pinmemberdalek930-Nov-06 9:06 
GeneralRe: Doing it with C# itself is much simpler... PinmemberEl Chubb11-Jul-08 17:40 
GeneralRe: Doing it with C# itself is much simpler... PinmembermicoxXx3-Aug-08 8:42 
GeneralCopyFromScreen Pinmembers.struck9-Sep-08 11:37 
GeneralRe: Doing it with C# itself is much simpler... Pinmembersnorkie9-Jul-09 10:54 
GeneralRe: Doing it with C# itself is much simpler... PinmemberB.V.Papadopoulos4-Aug-09 22:17 
QuestionVideo PinmemberRichardUci7-Nov-06 6:12 
GeneralDoesn't capture Layered (trasnparent, alpha blended) windows PinmemberJaykul9-Aug-05 9:14 
GeneralRe: Doesn't capture Layered (trasnparent, alpha blended) windows PinmemberJonFrost19-Feb-07 21:09 
GeneralNULL retured if I call the function 42 times Pinmemberraed23-Jun-05 4:10 
GeneralRe: NULL retured if I call the function 42 times PinmemberJaykul9-Aug-05 9:36 
GeneralMajor Memory Leak PinmemberFalazar6-May-05 10:58 
GeneralRe: Major Memory Leak PinmemberJisu7411-Nov-05 13:51 
GeneralRe: Major Memory Leak Pinmemberpsyphen21-May-09 4:46 
GeneralRe: Major Memory Leak Pinmemberjdohnoe7-Apr-10 14:36 
Generalthe Dll functions Pinmembertokyo_bme2-Mar-05 8:36 
GeneralRe: the Dll functions Pinmemberjjreilly2-Mar-05 9:18 
GeneralRe: the Dll functions Pinmembertokyo_bme4-Mar-05 21:30 
Generalscreen capture in web app Pinmembereheuer17-Feb-05 3:34 
GeneralRe: screen capture in web app PinmemberTimmy237-Mar-05 22:25 
GeneralBug with multiple monitors PinsussSherrod Segraves24-Sep-04 6:52 
GeneralBlack screen???Help!!!!!!! PinmemberWalkWorld5-Sep-04 20:05 
GeneralRe: Black screen???Help!!!!!!! Pinmemberdeonvs28-Jul-09 14:59 
QuestionOverlay? PinmemberJakub Florczyk22-Jul-03 23:59 
GeneralHelp with the remoting aspect PinmemberUTAMav11-Jul-03 7:26 
GeneralRe: Help with the remoting aspect PinmemberAgha Ali Raza26-Jul-03 14:12 
GeneralForm to bitmap (before it ever hits the screen) Pinmemberstantheman4-Mar-03 11:54 
GeneralAn alternative method PinmemberØyvind Rustan4-Mar-03 10:04 
QuestionHow much fun is it too... PinmemberCromwell4-Mar-03 9:53 
GeneralNot too shabby PinmemberRichard A. Johnn1-Mar-03 20:01 
General1000th Version of "Hello World" PinsussProgrammerABCDEFG28-Feb-03 10:30 
GeneralRe: 1000th Version of "Hello World" PinmemberTomáš Petříček28-Feb-03 12:32 
GeneralRe: 1000th Version of "Hello World" Pinmemberimaginejta1-Mar-03 6:23 
GeneralRe: 1000th Version of "Hello World" PinmemberThomas A. Dzieran2-Mar-03 13:42 
GeneralSlow Hog PinmemberShawn Stevens28-Feb-03 9:34 
GeneralRe: Slow Hog Pinmemberflipdoubt2-Mar-03 6:13 
GeneralRe: Slow Hog PinmemberShawn Stevens2-Mar-03 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 28 Feb 2003
Article Copyright 2002 by Agha Ali Raza
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid