C# 如何将屏幕截图保存在数量不断增加的循环中?
我目前有一个程序在循环中运行,如下所示:C# 如何将屏幕截图保存在数量不断增加的循环中?,c#,C#,我目前有一个程序在循环中运行,如下所示: else if (cmd == "streams") { Console.WriteLine("Please enter your monitors resolution"); Console.WriteLine("X:"); int xres = Convert.ToInt32(Console.Read
else if (cmd == "streams")
{
Console.WriteLine("Please enter your monitors resolution");
Console.WriteLine("X:");
int xres = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Y:");
int yres = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Thank you, recording now started. Note that this is still in beta. Close the software to stop recording");
Bitmap memoryImage;
memoryImage = new Bitmap(xres, yres);
Size s = new Size(memoryImage.Width, memoryImage.Height);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
for (; ; System.Threading.Thread.Sleep(20) )
{
//Send spacebar would go here
p.Send(green);
System.Threading.Thread.Sleep(20);
p.Send(regular);
System.Threading.Thread.Sleep(20);
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
string str = "";
str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
@"\Screenshot.png");
memoryImage.Save(str);
}
}
每次通过循环时,屏幕截图都被保存为名称“屏幕截图”。我希望每次截图时,数字都增加1,例如,截图0001,截图0002。非常感谢。您就快到了,只需将for循环更改为:
for (; ; System.Threading.Thread.Sleep(20) )
{
//Send spacebar would go here
p.Send(green);
System.Threading.Thread.Sleep(20);
p.Send(regular);
System.Threading.Thread.Sleep(20);
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
string str = "";
str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
@"\Screenshot.png");
memoryImage.Save(str);
}
…致:
for (var i = 0; ; i++) // <----- note 'i'
{
System.Threading.Thread.Sleep(20); // move sleep to here
//Send spacebar would go here
p.Send(green);
System.Threading.Thread.Sleep(20);
p.Send(regular);
System.Threading.Thread.Sleep(20);
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
string str = "";
str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
$@"\Screenshot{i}.png"); // use 'i' here
memoryImage.Save(str);
}
for(var i=0;;i++)//关于{i:d4}
?:)@约翰哦!是的,这看起来很像