嘿伙计我还有一个问题。我想把带有计时器的屏幕截图工作,但它只需要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");
}
答案 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");
}