Question

Hey Guys i have 1 More Problem . i want to take Screenshot with timer its works but it only take 1 screenshot after that it give me error i dont know what to do here is my CODE

i will show u error also

private static void GetScreenShot()
    {
        Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

        Graphics graphics = Graphics.FromImage(bitmap as Image);

        graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

        bitmap.Save("screenshot.jpeg", ImageFormat.Jpeg);

        }

    private void timer1_Tick(object sender, EventArgs e)
    {
        GetScreenShot();
        email.Send("HAMMAD");
        MessageBox.Show("DONE");

    }

enter image description here

Was it helpful?

Solution

Make a counter and then assign counter value to the filename.

Note 1: Bitmap parent class Image implements IDisposable interface so you could enclose the Bitmap class declaration within using block to make sure its disposal.

Note 2: Graphics class implements IDisposable interface so you could enclose the Graphics class declaration within using block to make sure its disposal.

Try This:

int counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    counter =counter+1;
    GetScreenShot(counter);
    email.Send("HAMMAD");
    MessageBox.Show("DONE");

}

private static void GetScreenShot(int counter)
{
    using(Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                                          Screen.PrimaryScreen.Bounds.Height))
   {
      using(Graphics graphics = Graphics.FromImage(bitmap as Image))
      {
         graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
      }
      bitmap.Save("screenshot"+counter+".jpeg", ImageFormat.Jpeg);       
   }
}

EDIT: for sending proper screenshot in email try this:

private void timer1_Tick(object sender, EventArgs e)
{
    counter =counter+1;
    GetScreenShot(counter);
    email.Send("screenshot"+counter+".jpeg");
    MessageBox.Show("DONE");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top