How to capture screen in .net

The following is the code sample for capturing screen of your computer using c# and

The example requires one Button control named btnCapture and one PictureBox control with named PictureBox1’. After running application when you click on button than picturebox show capture screen of your computer.


public static Bitmap CaptureScreen()
       Bitmap tempBmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
       System.Drawing.Graphics grp = System.Drawing.Graphics.FromImage(tempBmp);
                                   0, 0, Screen.PrimaryScreen.Bounds.Size,
       return tempBmp;
private void btnCapture_Click(object sender, EventArgs e)
      pictureBox1.Image = CaptureScreen();


Public Function CaptureScreen() As Bitmap
        Dim tempBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, _
                                  Screen.PrimaryScreen.Bounds.Height, _
        Dim grp As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(tempBmp)
        grp.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, _
                           0, 0, Screen.PrimaryScreen.Bounds.Size, _
        Return tempBmp
End Function
Private Sub btnCapture_Click(ByVal sender As Object, ByVal e As EventArgs)
        pictureBox1.Image = CaptureScreen()
End Sub


2 thoughts on “How to capture screen in .net”

  1. I try to use your code in a ASP.NET application.
    Some errors happened:

    The handle is invalid
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ComponentModel.Win32Exception: The handle is invalid

    line -> grp.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, _ Line 8338: 0, 0, Screen.PrimaryScreen.Bounds.Size, _ Line 8339: System.Drawing.CopyPixelOperation.SourceCopy)
    is possible use this in ASP.NET? How can I fix this problem?

Comments are closed.