C# Winform实现截图工具的示例代码
作者:搬砖的诗人Z
这篇文章主要为大家详细介绍了如何使用C# Winform制作一个简单的截图工具,从而实现截图功能,文中的示例代码讲解详细,有需要的可以参考下
效果图
实现代码
截图主要代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace OCR { public partial class ScreenBody : Form { public static Image myImage; private Graphics MainPainter; //主画面 private Pen pen; //画笔 private bool isDowned; //判断鼠标是否按下 private bool RectReady; //矩形是否绘制完成 private Image baseImage; //基本图形(原来的画面) private Rectangle Rect; //就是要保存的矩形 private Point downPoint; //鼠标按下的点 int tmpx; int tmpy; public ScreenBody() { InitializeComponent(); } private void ScreenBody_Load(object sender, EventArgs e) { try { this.WindowState = FormWindowState.Maximized; MainPainter = this.CreateGraphics(); pen = new Pen(Brushes.Blue); isDowned = false; baseImage = this.BackgroundImage; Rect = new Rectangle(); RectReady = false; } catch (Exception ex) { log4netHelper.Error("---报错方法--ScreenBody_Load"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); } } private void ScreenBody_MouseDown(object sender, MouseEventArgs e) { try { if (e.Button == MouseButtons.Left) { isDowned = true; if (RectReady == false) { Rect.X = e.X; Rect.Y = e.Y; downPoint = new Point(e.X, e.Y); } if (RectReady == true) { tmpx = e.X; tmpy = e.Y; } } if (e.Button == MouseButtons.Right) { this.Close(); return; } } catch (Exception ex) { log4netHelper.Error("---报错方法--ScreenBody_MouseDown"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); } } private void ScreenBody_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isDowned = false; RectReady = true; } } private void ScreenBody_MouseMove(object sender, MouseEventArgs e) { try { if (RectReady == false) { if (isDowned == true) { Image New = DrawScreen((Image)baseImage.Clone(), e.X, e.Y); MainPainter.DrawImage(New, 0, 0); New.Dispose(); } } if (RectReady == true) { if (Rect.Contains(e.X, e.Y)) { this.Cursor = Cursors.Hand; if (isDowned == true) { //和上一次的位置比较获取偏移量 Rect.X = Rect.X + e.X - tmpx; Rect.Y = Rect.Y + e.Y - tmpy; //记录现在的位置 tmpx = e.X; tmpy = e.Y; MoveRect((Image)baseImage.Clone(), Rect); } } else { this.Cursor = Cursors.Arrow; } } } catch (Exception ex) { log4netHelper.Error("---报错方法--ScreenBody_MouseMove"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); } } //画屏幕 private Image DrawScreen(Image back, int Mouse_x, int Mouse_y) { try { Graphics Painter = Graphics.FromImage(back); DrawRect(Painter, Mouse_x, Mouse_y); return back; } catch (Exception ex) { log4netHelper.Error("---报错方法--DrawScreen"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); return null; } } //画矩形 private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y) { int width = 0; int heigth = 0; try { if (Mouse_y < Rect.Y) { Rect.Y = Mouse_y; heigth = downPoint.Y - Mouse_y; } else { heigth = Mouse_y - downPoint.Y; } if (Mouse_x < Rect.X) { Rect.X = Mouse_x; width = downPoint.X - Mouse_x; } else { width = Mouse_x - downPoint.X; } } catch (Exception ex) { log4netHelper.Error("---报错方法--DrawRect"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); } finally { Rect.Size = new Size(width, heigth); Painter.DrawRectangle(pen, Rect); } } //移动矩形 private void MoveRect(Image image, Rectangle Rect) { try { Graphics Painter = Graphics.FromImage(image); Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height); MainPainter.DrawImage(image, 0, 0); image.Dispose(); } catch (Exception ex) { log4netHelper.Error("---报错方法--MoveRect"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); } } private void ScreenBody_MouseDoubleClick(object sender, MouseEventArgs e) { try { Image memory = new Bitmap(Rect.Width, Rect.Height); Graphics g = Graphics.FromImage(memory); g.CopyFromScreen(Rect.X + 1, Rect.Y + 1, 0, 0, new Size(Rect.Size.Width - 2, Rect.Size.Height - 2)); myImage = memory; this.Close(); this.Dispose(); } catch (Exception ex) { log4netHelper.Error("---报错方法--ScreenBody_MouseDoubleClick"); log4netHelper.Error(ex.Message); log4netHelper.Error(ex.StackTrace); log4netHelper.Error("---end---"); } } } }
HotKey,热键注册
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace OCR { public class HotKey { //如果函数执行成功,返回值不为0。 //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。 [DllImport("user32.dll", SetLastError = true)] public static extern bool RegisterHotKey( IntPtr hWnd, //要定义热键的窗口的句柄 int id, //定义热键ID(不能与其它ID重复) KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 Keys vk //定义热键的内容 ); [DllImport("user32.dll", SetLastError = true)] public static extern bool UnregisterHotKey( IntPtr hWnd, //要取消热键的窗口的句柄 int id //要取消热键的ID ); //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值) [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8 } } }
以上就是C# Winform实现截图工具的示例代码的详细内容,更多关于C# Winform截图的资料请关注脚本之家其它相关文章!