Home Search Contact us About us
Title Capture the Screen to an Image
Summary Capture the client area of a form to an Image type object. We also save this image to disk.
Contributor John McTainsh
Published 3-Jan-2004
Last updated 3-Jan-2004
Page rating   70% for 4 votes Useless Brilliant

Introduction

We will capture an image from the screen. It will be written to a System.Drawing.Image type object for manipulation or saving.

To save this image see Saving JPEG Images

To copy this image to the clipboard seeCopy Image to Clipboard

How to do it

An interop call is necessary to gain access to the BitBlt function from GDI32.dll. The following code will do this. The commented out code is not necessary

private const int SRCCOPY = 0x00CC0020;     // dest = source 

[System.Runtime.InteropServices.DllImport("GDI32.dll")]
private static extern Int32 BitBlt (IntPtr hdcDest, 
    Int32 nXDest, Int32 nYDest, Int32 nWidth, Int32 nHeight, IntPtr hdcSrc, 
    Int32 nXSrc, Int32 nYSrc, Int32 dwRop);

//BOOL BitBlt(
//  HDC hdcDest, // handle to destination DC
//  int nXDest,  // x-coord of destination upper-left corner
//  int nYDest,  // y-coord of destination upper-left corner
//  int nWidth,  // width of destination rectangle
//  int nHeight, // height of destination rectangle
//  HDC hdcSrc,  // handle to source DC
//  int nXSrc,   // x-coordinate of source upper-left corner
//  int nYSrc,   // y-coordinate of source upper-left corner
//  DWORD dwRop  // raster operation code
//  );

The following static method can be called to capture the Froms client area. It is static to can be called from amy where.

/// <summary>
/// Get a copy of the given Form client area to an Image object 
/// NOTE: 
///     1) Using BitBlt will the form and any overlaying dialogs ontop of the form.
/// </summary>
/// <param name="frm">Form to copy image from</param>
/// <returns>Image of current view</returns>
public static Image GetViewImage( Form frm )
{
    // Create an image that is compatible with the screen
    Graphics gr     = frm.CreateGraphics();
    Image img       = new Bitmap( frm.ClientSize.Width, frm.ClientSize.Height, gr );
    Graphics grMem  = Graphics.FromImage( img );

    // Get device contexts
    IntPtr dcScreen = gr.GetHdc();
    IntPtr dcMem    = grMem.GetHdc();

    // Copy the screen to the memory Image
    BitBlt( dcMem, 0, 0, frm.ClientSize.Width, frm.ClientSize.Height, dcScreen, 0, 0, SRCCOPY );
    gr.ReleaseHdc( dcScreen );
    grMem.ReleaseHdc( dcMem );
    return img;
}

Use

The first try will save the current form to a bitmap in the root folder. The second example saves some other form.

Note in both the following examples, SomeClass represents the class where the GetViewImage is located.

// Save this form
Image img = SomeClass.GetViewImage( this );
img.Save( "C:\\MyBitmap.BMP", System.Drawing.Imaging.ImageFormat.Bmp );

// Save some other form
Image imgOther = SomeClass.GetViewImage( frmNowShown );
imgOther.Save( "C:\\SaveFormNowShown.BMP", System.Drawing.Imaging.ImageFormat.Bmp );
Comments Date
Home Search Contact us About us