主窗体上一个截图按钮一个富文本框。点击截图按钮可以进行截图操作
代码如下:
public partial class MainFrm : Form { public MainFrm) { InitializeComponent); } private void btnCatch_Clickobject sender, EventArgs e) { this.Hide);//隐藏当前窗体 System.Threading.Thread.Sleep500);//让线程睡眠一段时间,窗体消失需要一点时间 ScrenFrm CatchForm = new ScrenFrm); Bitmap CatchBmp = new BitmapScreen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//新建一个和屏幕大小相同的图片 Graphics g = Graphics.FromImageCatchBmp); g.CopyFromScreennew Point0, 0), new Point0, 0), new SizeScreen.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.GetFormatDataFormats.Bitmap); if iData.GetDataPresentDataFormats.Bitmap)) { richTextBox1.PastemyFormat); 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 Disposebool disposing) { if disposing && components != null)) { components.Dispose); } base.Disposedisposing); } #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.SizeF6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size441, 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.EventHandlerthis.截图_Load); this.MouseClick += new System.Windows.Forms.MouseEventHandlerthis.截图_MouseClick); this.MouseDoubleClick += new System.Windows.Forms.MouseEventHandlerthis.截图_MouseDoubleClick); this.MouseDown += new System.Windows.Forms.MouseEventHandlerthis.截图_MouseDown); this.MouseMove += new System.Windows.Forms.MouseEventHandlerthis.截图_MouseMove); this.MouseUp += new System.Windows.Forms.MouseEventHandlerthis.截图_MouseUp); this.ResumeLayoutfalse); } #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 截图_Loadobject sender, EventArgs e) { originBitmap= new Bitmapthis.BackgroundImage);//BackgroundImage为全屏图片,我们另用变量来保存全屏图片 } private void 截图_MouseClickobject sender, MouseEventArgs e) { if e.Button == MouseButtons.Right) { this.DialogResult = DialogResult.OK; this.Close); } } private void 截图_MouseDownobject sender, MouseEventArgs e) { if e.Button == MouseButtons.Left) { if !catchStart) { catchStart = true; downPiont = new Pointe.X, e.Y); } } } private void 截图_MouseMoveobject sender, MouseEventArgs e) { if catchStart) { Bitmap destBitmap =Bitmap)originBitmap.Clone); Point newPoint = new Pointe.X,e.Y); Graphics g = Graphics.FromImagedestBitmap);//在刚才新建的图片上新建一个画板 Pen p = new PenColor.Blue, 1); int width = Math.Abse.X - downPiont.X), height = Math.Abse.Y - downPiont.Y);//获取矩形的长和宽 catchRect = new RectanglenewPoint.X < downPiont.X ? newPoint.X : downPiont.X, newPoint.Y < downPiont.Y ? newPoint.Y : downPiont.Y, width, height);//保存矩形 g.DrawRectanglep, catchRect);//将矩形画在这个画板上 g.Dispose);//释放目前的这个画板 p.Dispose); Graphics g1 = this.CreateGraphics);//重新新建一个Graphics类 //如果之前那个画板不释放,而直接g=this.CreateGraphics)这样的话无法释放掉第一次创建的g,因为只是把地址转到新的g了.如同string一样 g1 = this.CreateGraphics);//在整个全屏窗体上新建画板 g1.DrawImagedestBitmap, new Point0, 0));//将刚才所画的图片画到这个窗体上 //这个也可以属于二次缓冲技术,如果直接将矩形画在窗体上,会造成图片抖动并且会有无数个矩形. g1.Dispose); destBitmap.Dispose);//要及时释放,不然内存将会被大量消耗 } } private void 截图_MouseUpobject sender, MouseEventArgs e) { if e.Button == MouseButtons.Left) { if catchStart) { catchStart = false; catchFinished = true; } } } private void 截图_MouseDoubleClickobject sender, MouseEventArgs e) { if e.Button == MouseButtons.Left && catchFinished) { if catchRect.Containsnew Pointe.X, e.Y))) { Bitmap CatchedBmp = new BitmapcatchRect.Width, catchRect.Height);//新建一个于矩形等大的空白图片 Graphics g = Graphics.FromImageCatchedBmp); g.DrawImageoriginBitmap, new Rectangle0, 0, catchRect.Width, catchRect.Height), catchRect, GraphicsUnit.Pixel); //把orginBmp中的指定部分按照指定大小画在画板上 Clipboard.SetImageCatchedBmp);//将图片保存到剪贴板 g.Dispose); catchFinished = false; CatchedBmp.Dispose); this.DialogResult = DialogResult.OK; this.Close); } } } }