截图与计时器

时间:2014-03-18 07:36:34

标签: c# .net winforms screenshot

嘿伙计我还有一个问题。我想把带有计时器的屏幕截图工作,但它只需要1个屏幕截图,之后它给我错误我不知道该怎么做这里是我的代码

我也会显示你的错误

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

1 个答案:

答案 0 :(得分:3)

创建一个计数器,然后将计数器值分配给文件名。

注1: Bitmap父类Image实现了IDisposable接口,因此您可以将Bitmap类声明括在using block中确保它的处置。

注2: Graphics类实现了IDisposable接口,因此您可以将Graphics类声明括在using block中以确保其处置。

试试这个:

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);       
   }
}

编辑,要在电子邮件中发送适当的屏幕截图,请尝试以下操作:

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