博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单截图功能实现
阅读量:5327 次
发布时间:2019-06-14

本文共 6779 字,大约阅读时间需要 22 分钟。

 

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

代码如下:

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    {        ///         /// Required designer variable.        ///         private System.ComponentModel.IContainer components = null;        ///         /// Clean up any resources being used.        ///         /// true if managed resources should be disposed; otherwise, false.        protected override void Dispose(bool disposing)        {            if (disposing && (components != null))            {                components.Dispose();            }            base.Dispose(disposing);        }        #region Windows Form Designer generated code        ///         /// Required method for Designer support - do not modify        /// the contents of this method with the code editor.        ///         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

你可能感兴趣的文章
构建自己的项目管理方案
查看>>
利用pca分析fmri的生理噪声
查看>>
div水平居中且垂直居中
查看>>
epoll使用具体解释(精髓)
查看>>
AndroidArchitecture
查看>>
安装Endnote X6,但Word插件显示的总是Endnote Web"解决办法
查看>>
python全栈 计算机硬件管理 —— 硬件
查看>>
大数据学习
查看>>
简单工厂模式
查看>>
Delphi7编译的程序自动中Win32.Induc.a病毒的解决办法
查看>>
Objective-C 【关于导入类(@class 和 #import的区别)】
查看>>
倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-点击运行按钮进入到运行状态报错Error starting TwinCAT System怎么办 AdsWarning1823怎么办...
查看>>
【转】javascript 中的很多有用的东西
查看>>
Centos7.2正常启动关闭CDH5.16.1
查看>>
Android 监听返回键、HOME键
查看>>
Android ContentProvider的实现
查看>>
sqlserver 各种判断是否存在(表名、函数、存储过程等)
查看>>
给C#学习者的建议 - CLR Via C# 读后感
查看>>
Recover Binary Search Tree
查看>>
Java 实践:生产者与消费者
查看>>