1、C#实现屏幕截图运行效果:代码: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.Drawing.Drawing2D;namespace Test.UIFrmpublic class Form2 : Form#region 窗体设计器代码private System.Co
2、mponentModel.IContainer components = null;protected override void Dispose(bool disposing)if (disposing)if (components != null)components.Dispose();base.Dispose(disposing);private void InitializeComponent()this.tiptext = new System.Windows.Forms.Label();this.btnOk = new System.Windows.Forms.Button();
3、this.btnSave = new System.Windows.Forms.Button();this.btnCancel = new System.Windows.Forms.Button();this.SuspendLayout();/ / tiptext/ this.tiptext.BackColor = System.Drawing.Color.DimGray;this.tiptext.ForeColor = System.Drawing.Color.Transparent;this.tiptext.Location = new System.Drawing.Point(86, 9
4、);this.tiptext.Name = “tiptext“;this.tiptext.Size = new System.Drawing.Size(85, 13);this.tiptext.TabIndex = 0;this.tiptext.Text = “1200*1200“;this.tiptext.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;this.tiptext.Visible = false;/ / btnOk/ this.btnOk.Location = new System.Drawing.Point(0
5、, 0);this.btnOk.Name = “btnOk“;this.btnOk.Size = new System.Drawing.Size(40, 20);this.btnOk.TabIndex = 22;this.btnOk.Text = “完成“;this.btnOk.Visible = false;this.btnOk.Click += new System.EventHandler(this.btnOk_Click);/ / btnSave/ this.btnSave.Location = new System.Drawing.Point(0, 0);this.btnSave.N
6、ame = “btnSave“;this.btnSave.Size = new System.Drawing.Size(40, 20);this.btnSave.TabIndex = 1;this.btnSave.Text = “保存“;this.btnSave.Visible = false;this.btnSave.Click += new System.EventHandler(this.btnSave_Click);/ / btnCancel/ this.btnCancel.Location = new System.Drawing.Point(0, 0);this.btnCancel
7、.Name = “btnCancel“;this.btnCancel.Size = new System.Drawing.Size(40, 20);this.btnCancel.TabIndex = 3;this.btnCancel.Text = “取消“;this.btnCancel.Visible = false;this.btnCancel.Click += new EventHandler(btnCancel_Click);/ / Form2/ this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize
8、 = new System.Drawing.Size(456, 320);this.Controls.Add(this.btnCancel);this.Controls.Add(this.tiptext);this.Controls.Add(this.btnOk);this.Controls.Add(this.btnSave);this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;this.Name = “Form2“;this.ShowInTaskbar = false;this.StartPosition = Sy
9、stem.Windows.Forms.FormStartPosition.WindowsDefaultBounds;this.Text = “鼠标截屏 “;this.TopMost = true;this.WindowState = System.Windows.Forms.FormWindowState.Maximized;this.DoubleClick += new System.EventHandler(this.MouseCaptureForm_DoubleClick);this.KeyDown += new System.Windows.Forms.KeyEventHandler(
10、this.MouseCaptureForm_KeyDown);this.ResumeLayout(false);#endregionprivate Point pot;private Rectangle area = Rectangle.Empty;private Image img;private int index = -1;private System.Windows.Forms.Label tiptext;public Form2()this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;this.Backgroun
11、dImage = this.GetScreen();InitializeComponent();this.DoubleBuffered = true;#region 操作栏private System.Windows.Forms.Button btnOk;private System.Windows.Forms.Button btnSave;private void btnSave_Click(object sender, EventArgs e)this.SaveToFile();private void btnOk_Click(object sender, EventArgs e)this
12、.SaveToClipBoard();private void btnCancel_Click(object sender, EventArgs e)this.Close();private void ToolBars(bool show)if (show)btnCancel.Location = new Point(this.area.X + this.area.Width - btnCancel.Width, this.area.Y + this.area.Height + 10);btnOk.Location = new Point(this.area.X + this.area.Wid
13、th - btnOk.Width - btnCancel.Width, this.area.Y + this.area.Height + 10);btnSave.Location = new Point(this.area.X + this.area.Width - btnOk.Width - btnSave.Width - btnCancel.Width, this.area.Y + this.area.Height + 10);btnOk.Visible = show;btnSave.Visible = show;btnCancel.Visible = show;#endregion#re
14、gion 获取屏幕/ 引用 APISystem.Runtime.InteropServices.DllImportAttribute(“gdi32.dll“)public static extern bool BitBlt(IntPtr hdcDest, /目标设备的句柄 int nXDest, / 目标对象的左上角的 X 坐标 int nYDest, / 目标对象的左上角的 X 坐标 int nWidth, / 目标对象的矩形的宽度 int nHeight, / 目标对象的矩形的长度 IntPtr hdcSrc, / 源设备的句柄 int nXSrc, / 源对象的左上角的 X 坐标 int
15、 nYSrc, / 源对象的左上角的 X 坐标 System.Int32 dwRop / 光栅的操作值 );private Bitmap GetScreen()/建立屏幕 GraphicsGraphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);/根据屏幕大小建立位图Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grpScreen);/建立位图相关 GraphicsGraphics grpBitmap
16、 = Graphics.FromImage(bitmap);/建立屏幕上下文IntPtr hdcScreen = grpScreen.GetHdc();/建立位图上下文IntPtr hdcBitmap = grpBitmap.GetHdc();/将屏幕捕获保存在图位中BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcScreen, 0, 0, 0x00CC0020);/关闭位图句柄grpBitmap.ReleaseHdc(hdcBitmap);/关闭屏幕句柄grpScreen.ReleaseHdc(hdcScreen);/释放位图对
17、像grpBitmap.Dispose();/释放屏幕对像grpScreen.Dispose();/返回捕获位图return bitmap;#endregion#region 绘制窗口/ / 设置鼠标方案 / private void SetCursor()Cursor cr = Cursors.Default;if (index = 1 | index = 5)cr = Cursors.SizeNWSE;else if (index = 2 | index = 6)cr = Cursors.SizeNS;else if (index = 3 | index = 7)cr = Cursors.S
18、izeNESW;else if (index = 4 | index = 8)cr = Cursors.SizeWE;else if (index = 0)cr = Cursors.SizeAll;Cursor.Current = cr;protected override void OnPaint(PaintEventArgs e)SuspendLayout();base.OnPaint(e);Rectangle tmp = this.GetSelectedRectangle();e.Graphics.DrawRectangle(new Pen(Color.Green), tmp);this
19、.tiptext.Location = new Point(tmp.X, tmp.Y - this.tiptext.Height - 4);/maskGraphicsPath path = new GraphicsPath();int x = tmp.X, y = tmp.Y, w = tmp.Width, h = tmp.Height;int sw = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, sh = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;path
20、.AddRectangle(new Rectangle(0, 0, sw, y);path.AddRectangle(new Rectangle(0, y, x, h);path.AddRectangle(new Rectangle(x + w, y, sw - x - w, h);path.AddRectangle(new Rectangle(0, y + h, sw, sh - y - h);Region region = new Region(path);SolidBrush brush = new SolidBrush(Color.FromArgb(100, Color.Black);
21、e.Graphics.FillRegion(brush, region);if (this.area != Rectangle.Empty)for (int i = 1; i / 获取选择的矩形区域/ / private Rectangle GetSelectedRectangle()int x1 = this.area.X, y1 = this.area.Y, x2 = this.area.X + this.area.Width, y2 = this.area.Y + this.area.Height;Rectangle tmp = new Rectangle(x1, y1, Math.Ab
22、s(this.area.Width), Math.Abs(this.area.Height);if (x1 x2)tmp.X = x2;if (y1 y2)tmp.Y = y2;return tmp;private Rectangle GetHandleRect(int index)int x, y, xCenter, yCenter;xCenter = area.X + area.Width / 2;yCenter = area.Y + area.Height / 2;x = area.X;y = area.Y;switch (index)case 1:x = area.X;y = area
23、.Y;break;case 2:x = xCenter;y = area.Y;break;case 3:x = area.Right;y = area.Y;break;case 4:x = area.Right;y = yCenter;break;case 5:x = area.Right;y = area.Bottom;break;case 6:x = xCenter;y = area.Bottom;break;case 7:x = area.X;y = area.Bottom;break;case 8:x = area.X;y = yCenter;break;Point point = n
24、ew Point(x, y);return new Rectangle(point.X - 3, point.Y - 3, 6, 6);private int GetSelectedHandle(Point p)int index = -1;for (int i = 1; i Screen.PrimaryScreen.Bounds.Right)right = Screen.PrimaryScreen.Bounds.Right;if (top Screen.PrimaryScreen.Bounds.Bottom)bottom = Screen.PrimaryScreen.Bounds.Botto
25、m;area.X = left;area.Y = top;area.Width = right - left;area.Height = bottom - top;/截取选择区域图片Bitmap bm = new Bitmap(this.BackgroundImage);this.img = bm.Clone(this.area, System.Drawing.Imaging.PixelFormat.Format32bppArgb);this.Close();private void SaveToClipBoard()this.CaptureImage();Clipboard.SetDataO
26、bject(this.img, true);private void SaveToFile()SaveFileDialog saveImageDialog = new SaveFileDialog();saveImageDialog.Title = “保存图片.“;saveImageDialog.Filter = “png|*.png|jpeg|*.jpg|bmp|*.bmp|gif|*.gif“;if (saveImageDialog.ShowDialog() = DialogResult.OK)string fileName = saveImageDialog.FileName.ToStr
27、ing();if (!string.IsNullOrEmpty(fileName)string fileExtName = fileName.Substring(fileName.LastIndexOf(“.“) + 1).ToString();System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Png;if (!string.IsNullOrEmpty(fileExtName)switch (fileExtName.ToLower()case “jpg“:imgformat = S
28、ystem.Drawing.Imaging.ImageFormat.Jpeg;break;case “bmp“:imgformat = System.Drawing.Imaging.ImageFormat.Bmp;break;case “gif“:imgformat = System.Drawing.Imaging.ImageFormat.Gif;break;case “png“:imgformat = System.Drawing.Imaging.ImageFormat.Png;break;default:break;trythis.CaptureImage();this.img.Save(fileName, imgformat);catchMessageBox.Show(“保存失败!“);#endregionpublic delegate void Finised(Image image);/ / 截图操作完成事件/ public event Finised OnFinised;protected override void OnClosing(CancelEventArgs e)base.OnClosing(e);if (this.OnFinised != null)this.OnFinised(this.img);private Button btnCancel;