设计器代码:
namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(101, 45); this.button1.TabIndex = 0; this.button1.Text = "全屏截图"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 138); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(101, 12); this.label1.TabIndex = 1; this.label1.Text = "点击按钮2s后截图"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 171); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(173, 12); this.label2.TabIndex = 2; this.label2.Text = "图片存放在C://屏幕截图目录下"; // // button2 // this.button2.Location = new System.Drawing.Point(242, 11); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(114, 46); this.button2.TabIndex = 3; this.button2.Text = "打开目录"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(129, 11); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(94, 46); this.button3.TabIndex = 4; this.button3.Text = "自定义截图"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Location = new System.Drawing.Point(242, 63); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(114, 54); this.button4.TabIndex = 5; this.button4.Text = "设置截图标记"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // textBox1 // this.textBox1.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.textBox1.ForeColor = System.Drawing.Color.Red; this.textBox1.Location = new System.Drawing.Point(14, 65); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(209, 52); this.textBox1.TabIndex = 6; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(382, 204); this.Controls.Add(this.textBox1); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.TextBox textBox1; } }
Form:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //全屏幕截图 private void button1_Click(object sender, EventArgs e) { string mark = textBox1.Text; this.WindowState = FormWindowState.Minimized;//最小化当前窗口 Thread.Sleep(2000);//线程暂停2000ms //创建存放目录 if (!Directory.Exists("C:\\屏幕截图")) { DirectoryInfo dir = new DirectoryInfo("C:\\屏幕截图"); dir.Create(); } Bitmap bmp2 = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height); Graphics g2 = Graphics.FromImage(bmp2); g2.CopyFromScreen(new Point(0, 0), new Point(0, 0), bmp2.Size); string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); time = System.Text.RegularExpressions.Regex.Replace(time, @"[^0-9]+", "");//提取数字 string fileName = time + ".bmp"; //创建文件名 bmp2.Save("c:\\屏幕截图\\" + fileName); //保存为文件 ,注意格式是否正确. bmp2.Dispose();//关闭对象 g2.Dispose();//关闭画笔 //为图片添加标记 Image image = Image.FromFile("c:\\屏幕截图\\" + fileName); Bitmap bitmap = new Bitmap(image, image.Width, image.Height); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //字体大小 float fontSize = 40.0f; //文本的长度 float textWidth = mark.Length * fontSize; //下面定义一个矩形区域,以后在这个矩形里画上白底黑字 float rectX = 120; float rectY = 200; float rectWidth = mark.Length * (fontSize + 40); float rectHeight = fontSize + 40; //声明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight); //定义字体 System.Drawing.Font font = new System.Drawing.Font("微软雅黑", fontSize, System.Drawing.FontStyle.Bold); //font.Bold = true; //白笔刷,画文字用 Brush whiteBrush = new SolidBrush(System.Drawing.Color.Red); //黑笔刷,画背景用 //Brush blackBrush = new SolidBrush(Color.Black); //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight); g.DrawString(mark, font, whiteBrush, textArea); //输出方法一:将文件生成并保存到C盘 string path = "c:\\屏幕截图\\MK" + fileName; bitmap.Save(path); g.Dispose(); bitmap.Dispose(); image.Dispose(); this.WindowState = FormWindowState.Normal;//截图完毕复原窗口 } //打开存储路径 private void button2_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("C:\\屏幕截图"); } //自定义大小截图 private void button3_Click(object sender, EventArgs e) { } //为图片加标记 private void button4_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } } }