Perform a Screen Capture : Screen « 2D Graphics « C# / C Sharp






Perform a Screen Capture

  


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

public class DesktopCapture {

    [DllImport("user32.dll")]
    private extern static IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    private extern static IntPtr GetDC(IntPtr windowHandle);

    [DllImport("gdi32.dll")]
    private extern static IntPtr GetCurrentObject(IntPtr hdc,
      ushort objectType);

    [DllImport("user32.dll")]
    private extern static void ReleaseDC( IntPtr hdc );

    const int OBJ_BITMAP = 7;

    public static Bitmap Capture() {
        IntPtr desktopWindow = GetDesktopWindow();
        IntPtr desktopDC = GetDC( desktopWindow );
        IntPtr desktopBitmap = GetCurrentObject(desktopDC, OBJ_BITMAP);
        Bitmap desktopImage = Image.FromHbitmap( desktopBitmap );
        ReleaseDC(desktopDC);
        return desktopImage;
    }
}

   
  








Related examples in the same category

1.Get Screen ResolutionGet Screen Resolution
2.Takes a screen shot saves it to the specified directory and returns the full file path