简单截图功能实现

主窗体上一个截图按钮一个富文本框。点击截图按钮可以进行截图操作

代码如下:

public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();        
        }

        private void btnCatch_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏当前窗体
            System.Threading.Thread.Sleep(500);//让线程睡眠一段时间,窗体消失需要一点时间
            ScrenFrm CatchForm = new ScrenFrm();
            Bitmap CatchBmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//新建一个和屏幕大小相同的图片    
            Graphics g = Graphics.FromImage(CatchBmp);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));//保存全屏图片
            CatchForm.BackgroundImage = CatchBmp;//将Catch窗体的背景设为全屏时的图片
            if (CatchForm.ShowDialog() == DialogResult.OK)
            {//如果Catch窗体结束,就将剪贴板中的图片放到信息发送框中
                IDataObject iData = Clipboard.GetDataObject();
                DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
                if (iData.GetDataPresent(DataFormats.Bitmap))
                {
                    richTextBox1.Paste(myFormat);
                    Clipboard.Clear();//清除剪贴板中的对象
                }
                this.Show();//重新显示窗体
            }
        }
    }

弹出窗体代码如下:

 public partial class ScrenFrm : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;


        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }


        #region Windows Form Designer generated code


        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ScrenFrm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(441, 315);
            this.DoubleBuffered = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "ScrenFrm";
            this.Text = "截图";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.截图_Load);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.截图_MouseClick);
            this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.截图_MouseDoubleClick);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.截图_MouseDown);
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.截图_MouseMove);
            this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.截图_MouseUp);
            this.ResumeLayout(false);


        }


        #endregion
        
        public ScrenFrm()
        {
            InitializeComponent();
        }


        private Point downPiont = Point.Empty;//记录鼠标摁下的点
        private bool catchFinished = false;//用来表示截图是否完成
        private bool catchStart = false;    //截取开始
        private Bitmap originBitmap;       //用来保存原始图像
        private Rectangle catchRect;       //用来保存存取的矩形


        private void 截图_Load(object sender, EventArgs e)
        {
          originBitmap= new Bitmap(this.BackgroundImage);//BackgroundImage为全屏图片,我们另用变量来保存全屏图片
        }


        private void 截图_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }


        private void 截图_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!catchStart)
                { catchStart = true;
                downPiont = new Point(e.X, e.Y);
                }
            }
        }


        private void 截图_MouseMove(object sender, MouseEventArgs e)
        {
            if (catchStart)
            {
                Bitmap destBitmap =(Bitmap)originBitmap.Clone();
                Point newPoint = new Point(e.X,e.Y);
                Graphics g = Graphics.FromImage(destBitmap);//在刚才新建的图片上新建一个画板
                Pen p = new Pen(Color.Blue, 1);
                int width = Math.Abs(e.X - downPiont.X), height = Math.Abs(e.Y - downPiont.Y);//获取矩形的长和宽              
                catchRect = new Rectangle(newPoint.X < downPiont.X ? newPoint.X : downPiont.X, newPoint.Y < downPiont.Y ? newPoint.Y : downPiont.Y, width, height);//保存矩形
                g.DrawRectangle(p, catchRect);//将矩形画在这个画板上
                g.Dispose();//释放目前的这个画板
                p.Dispose();
                Graphics g1 = this.CreateGraphics();//重新新建一个Graphics类
                //如果之前那个画板不释放,而直接g=this.CreateGraphics()这样的话无法释放掉第一次创建的g,因为只是把地址转到新的g了.如同string一样
                g1 = this.CreateGraphics();//在整个全屏窗体上新建画板
                g1.DrawImage(destBitmap, new Point(0, 0));//将刚才所画的图片画到这个窗体上
                //这个也可以属于二次缓冲技术,如果直接将矩形画在窗体上,会造成图片抖动并且会有无数个矩形.
                g1.Dispose();
                destBitmap.Dispose();//要及时释放,不然内存将会被大量消耗
            }
        }


        private void 截图_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (catchStart)
                {
                    catchStart = false;
                    catchFinished = true;
                }
            }
        }


        private void 截图_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && catchFinished)
            {
                if (catchRect.Contains(new Point(e.X, e.Y)))
                {
                    Bitmap CatchedBmp = new Bitmap(catchRect.Width, catchRect.Height);//新建一个于矩形等大的空白图片
                    Graphics g = Graphics.FromImage(CatchedBmp);
                    g.DrawImage(originBitmap, new Rectangle(0, 0, catchRect.Width, catchRect.Height), catchRect, GraphicsUnit.Pixel);
                    //把orginBmp中的指定部分按照指定大小画在画板上
                    Clipboard.SetImage(CatchedBmp);//将图片保存到剪贴板
                    g.Dispose();
                    catchFinished = false;
                    CatchedBmp.Dispose();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        } 
    } 



原文地址:https://www.cnblogs.com/jiangu66/p/3225905.html