做了一个winform程序,可以手动截取整个屏幕,也可以自动定时截取屏幕,并发送到指定的邮箱。
窗体界面如下:


截取屏幕和发送邮件代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Net.Mail;
using System.Net;
namespace Snapshot
{
public class Common
{
/// <summary>
/// 截图屏幕
/// </summary>
public static void ScreenSnapshot(string filepath)
{
Screen scr = Screen.PrimaryScreen;
Rectangle rc = scr.Bounds;
int iWidth = rc.Width;
int iHeight = rc.Height;
Image myImage = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(myImage);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
myImage.Save(filepath);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="toAddress">收件人</param>
/// <param name="mailSubject">邮件主题</param>
/// <param name="mailBody">邮件内容</param>
/// <param name="mailAttachList">附件路径</param>
/// <returns></returns>
public static bool SendMail(string toAddress, string mailSubject, string mailBody, List<string> mailAttachList)
{
MailAddress from = new MailAddress("xxx@163.com", "J.L.C"); //设置发件人信箱,及显示名字
MailAddress to = new MailAddress(toAddress, "JLC"); //设置收件人信箱,及显示名字
using (MailMessage oMail = new MailMessage(from, to)) //创建一个MailMessage对象
{
oMail.Subject = mailSubject; //邮件标题
oMail.Body = mailBody; //邮件内容
oMail.IsBodyHtml = true; //指定邮件格式,支持HTML格式
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件采用的编码
oMail.Priority = MailPriority.High;//设置邮件的优先级为高
foreach (string attach in mailAttachList)//附件路径
oMail.Attachments.Add(new Attachment(attach));
//发送邮件服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com"; //指定邮件服务器
client.Credentials = new NetworkCredential("xxx@163.com", "xxx");//指定发件人邮箱,及密码
try
{
client.Send(oMail); //发送邮件
}
catch
{
return false;
}
}
return true;
}
}
}
窗体中后台cs代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Snapshot
{
public partial class FrmSnapshot : Form
{
System.Windows.Forms.Timer timer;
public FrmSnapshot()
{
InitializeComponent();
this.Resize += new EventHandler(FormCopyScreen_Resize);
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
toolStripMenuItemExit.MouseDown += new MouseEventHandler(toolStripMenuItemExit_MouseDown);
toolStripStatusLabelTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
trackBar1.Minimum = 1;
trackBar1.Maximum = 60;
timer = new System.Windows.Forms.Timer();
timer.Tick += new EventHandler(timer_Tick);
}
/// <summary>
/// 生成的图片文件名
/// </summary>
private string imgFile
{
get
{
return DateTime.Now.ToString("yyyyMMdd_HHmmssfff") + ".jpg";
}
}
/// <summary>
/// 定时自动截取屏幕,并发邮件
/// </summary>
private void btnStart_Click(object sender, EventArgs e)
{
if (btnStart.Text == "执行")
{
if (chkMin.Checked)
HideForm();
timer.Interval = 1000 * 60 * trackBar1.Value;
timer.Start();
btnStart.Text = "停止";
toolStripStatusLabelMsg.Text = string.Format("正在每隔{0}分,自动截图中...", trackBar1.Value);
}
else
{
timer.Stop();
btnStart.Text = "执行";
toolStripStatusLabelMsg.Text = "";
}
}
void SendMail(string img)
{
Common.SendMail("xxx@qq.com", "pic", "", new List<string>() { img });
}
/// <summary>
/// 手动截取屏幕
/// </summary>
private void btnCopyScreen_Click(object sender, EventArgs e)
{
HideForm();
string img = imgFile;
Common.ScreenSnapshot(img);
ShowForm();
toolStripStatusLabelMsg.Text = "截图成功:" + img;
//发邮件时间稍微久了点,窗体没反应,开一个线程执行
Thread th = new Thread(delegate() { SendMail(img); });
th.Start();
}
/// <summary>
/// 定时执行
/// </summary>
private void timer_Tick(object sender, EventArgs e)
{
string img = imgFile;
Common.ScreenSnapshot(img);
Common.SendMail("xxx@qq.com", "pic", "", new List<string>() { img });
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
lblValue.Text = trackBar1.Value.ToString();
}
/// <summary>
/// 点击托盘图标显示窗体
/// </summary>
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowForm();
}
/// <summary>
/// 托盘鼠标右键退出
/// </summary>
private void toolStripMenuItemExit_MouseDown(object sender, MouseEventArgs e)
{
Application.Exit();
}
private void FormCopyScreen_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
HideForm();
}
}
/// <summary>
/// 隐藏窗体
/// </summary>
private void HideForm()
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
notifyIcon1.Visible = true;
}
/// <summary>
/// 显示窗体
/// </summary>
private void ShowForm()
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
}

浙公网安备 33010602011771号