The following is the code sample for capturing screen of your computer using c# and vb.net.
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.
[C#]
public static Bitmap CaptureScreen() { Bitmap tempBmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Graphics grp = System.Drawing.Graphics.FromImage(tempBmp); grp.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, System.Drawing.CopyPixelOperation.SourceCopy); return tempBmp; } private void btnCapture_Click(object sender, EventArgs e) { pictureBox1.Image = CaptureScreen(); }
[VB.Net]
Public Function CaptureScreen() As Bitmap Dim tempBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, _ Screen.PrimaryScreen.Bounds.Height, _ System.Drawing.Imaging.PixelFormat.Format32bppArgb) 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, _ System.Drawing.CopyPixelOperation.SourceCopy) Return tempBmp End Function Private Sub btnCapture_Click(ByVal sender As Object, ByVal e As EventArgs) pictureBox1.Image = CaptureScreen() End Sub
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?
Regards
you cant do it