我的程序应该每隔30秒拍摄一次屏幕截图,然后将它们存储在隐藏文件夹C:\ SysApp中。我是编码的新手,而Visual Studio并没有说有任何错误,所以我很困惑。也许你可以帮我吗?谢谢!
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Drawing;
namespace screenshothoop
{
static class Program
{
static void Main()
{
//-----this code will make your program to automatically execute as computer starts----
try
{
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Assembly curAssembly = Assembly.GetExecutingAssembly();
key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
Console.WriteLine(curAssembly.GetName());
}
catch { }
//------------------
//------------screenshot loop takes screenshots after 30 sec-----------
int n = 0;
while (n == 0)
{
Thread.Sleep(30000);
OnTimedEvent();
}
//-------------------------
}// main body ends !
public static string st = "";
public static string date = "";
public static string month = "";
public static string year = "";
public static string time = "";
public static string hour = "";
public static string min = "";
public static string sec = "";
private static void OnTimedEvent()
{
st = DateTime.Today.Date.ToString();
time = DateTime.Now.TimeOfDay.ToString();
hour = DateTime.Now.Hour.ToString();
min = DateTime.Now.Minute.ToString();
sec = DateTime.Now.Second.ToString();
date = DateTime.Today.Day.ToString();
month = DateTime.Today.Month.ToString();
year = DateTime.Today.Year.ToString();
Console.WriteLine("The Elapsed event was raised at {0}_{1}_{2} at time {3}_{4}_{5} ", date, month, year, hour, min, sec);
Bitmap memoryImage;
memoryImage = new Bitmap(1000, 800);
Size s = new Size(memoryImage.Width, memoryImage.Height);
// Create graphics
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
// Copy data from screen
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
string str = "";
//------------creating directory--------
if (Directory.Exists("C:\\SysApp"))
{
Console.WriteLine("directory exits");
}
else
{
Directory.CreateDirectory("C:\\SysApp");
File.SetAttributes("C:\\SysApp", FileAttributes.Hidden);
Console.WriteLine("new directory created");
}
//---------------------------------------
str = string.Format("d:\\screenshotn\\screen {0}_{1}.png", date + month + year, hour + min + sec);
//------------
try
{
memoryImage.Save(str);
}
catch (Exception er)
{
Console.WriteLine("Sorry, there was an error: " + er.Message);
}
//---------------------------------------------------------
}
}
}

答案 0 :(得分:6)
首先检查C:\SysApp
是否存在,如果不存在,则创建它。到现在为止还挺好。然后将图像写入D:\screenshotn\Etc.
,这可能不存在。或者如果确实存在,则检查图像的错误文件夹。 :)
更改此行:
str = string.Format("d:\\screenshotn\\screen {0}_{1}.png", date + month + year, hour + min + sec);
到
str = string.Format("C:\\SysApp\\screen {0}_{1}.png", date + month + year, hour + min + sec);
答案 1 :(得分:1)
您的代码可以更清楚地概括为:
memoryImage = new Bitmap(1000, 800);
...
str = string.Format("d:\\screenshotn\\screen {0}_{1}.png", date + month + year, hour + min + sec);
...
memoryImage.Save(str);
如果您在线查找解释Bitmap.Save
所做内容的资源,找到here,您会看到第一个参数描述了文件写入磁盘的位置。
由于str
设置为“d:\ screenshotn \ sc ...”,因此它将被写入您之前设置的其他驱动器。