C# full screen capture + add text mark to picture

Designer code:

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variables.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up all resources in use.
        /// </summary>
        /// <param name="disposing">true if the managed resource should be released; otherwise, false. </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Forms Designer Generated Code

        /// <summary>
        /// Designer supports required methods - don't
        /// Use a code editor to modify the content of this method.
        /// </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 = "Full Screenshot";
            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 = "Screenshot after clicking the button 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 = "The picture is stored in the C://screenshot directory";
            //
            // 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 = "Open Directory";
            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 = "Custom Screenshot";
            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 = "Set Screenshot Marker";
            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();
        }

        //full screen shot
        private void button1_Click(object sender, EventArgs e)
        {
            string mark = textBox1.Text;
            this.WindowState = FormWindowState.Minimized;//Minimize the current window
            Thread.Sleep(2000);//Thread pause for 2000ms
            //create storage directory
            if (!Directory.Exists("C:\\screenshot"))
            {
                DirectoryInfo dir = new DirectoryInfo("C:\\screenshot");
                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"; //Create file name
            bmp2.Save("c:\\screenshot\\" + fileName); //Save as a file, pay attention to whether the format is correct.
            bmp2.Dispose();//Close the object
            g2.Dispose();//Close the brush

            //Add a marker to the image
            Image image = Image.FromFile("c:\\screenshot\\" + fileName);
            Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //font size  
            float fontSize = 40.0f;
            // length of text  
            float textWidth = mark.Length * fontSize;
            //Define a rectangular area below, and then draw black characters on a white background in this rectangle  
            float rectX = 120;
            float rectY = 200;
            float rectWidth = mark.Length * (fontSize + 40);
            float rectHeight = fontSize + 40;
            //declare the rectangle  
            RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);
            //define the font  
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", fontSize, System.Drawing.FontStyle.Bold);
            //font.Bold = true;  
            //white brush, used to draw text  
            Brush whiteBrush = new SolidBrush(System.Drawing.Color.Red);
            //Black brush, used to draw background  
            //Brush blackBrush = new SolidBrush(Color.Black);     
            //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);  
            g.DrawString(mark, font, whiteBrush, textArea);
            //Output method 1: Generate and save the file to the C drive  
            string path = "c:\\screenshot\\MK" + fileName;
            bitmap.Save(path);
            g.Dispose();
            bitmap.Dispose();
            image.Dispose();
            this.WindowState = FormWindowState.Normal;//Restore the window after the screenshot is completed

        }

        //Open the storage path
        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("C:\\screenshot");
        }

        //custom size screenshot
        private void button3_Click(object sender, EventArgs e)
        {

        }

        // mark the image
        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480248&siteId=291194637