Capture screenshot using C#

In this article we will see how to capture a screenshot of the computer screen and save it to a png image.
First, create a Windows Forms application and add a button and pictureBox to the Form. And set SizeMode of pictureBox to StretchImage.
In CaptureButton click event add this code:

private void button1_Click(object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics gr = Graphics.FromImage(bmp);
    gr.CopyFromScreen(0, 0, 0, 0, bmp.Size);
    pictureBox1.Image = bmp;
    bmp.Save("img.png",System.Drawing.Imaging.ImageFormat.Png);
}

Now run the application and click Capture, this is the output:


The image is saved to the application path ...\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug".

For more explanation, check this video:


Capture screenshot using C# Capture screenshot using C# Reviewed by Bloggeur DZ on 11:00 Rating: 5

Aucun commentaire:

Fourni par Blogger.