使用方法如下:

 private void button1_Click(object sender, EventArgs e)
{
s.GerScreenFormRectangle(); }
private Zgke.CopyScreen s;
private void Form1_Load(object sender, EventArgs e)
{
s = new Zgke.CopyScreen();
s.GetScreenImage+=new Zgke.CopyScreen.GetImage(s_GetScreenImage);
} void s_GetScreenImage(Image p_Image)
{
pictureBox1.Image = p_Image;
}

当按下BUTTON1的时候可以在屏幕上选择一个矩形进行截图

全部的类

先建立个新项目 为WINDOWS库类.这里因为需要全局钩子..我在WINFORM里不知道为什么设置不了全局钩子.但在DLL里就可以.....可恶的C#........

复制下面代码到CLASS1.CS里

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms; namespace Zgke
{
public class API
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid); public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern int GetCurrentThreadId(); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhook); public enum WindowsHookCodes
{
WH_MSGFILTER = (-1),
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
} } /// <summary>
/// 一个根据矩形截图类
/// zgke@Sina.com
/// qq:116149
/// </summary>
public class CopyScreen
{
/// <summary>
/// 屏幕大小
/// </summary>
private Size ScreenSize { get { return Screen.PrimaryScreen.Bounds.Size; } }
/// <summary>
/// 鼠标位置
/// </summary>
private Point MousePoint { get { return Cursor.Position; } }
/// <summary>
/// 私有方法获取屏幕图形(全部图形)
/// </summary>
public Bitmap ScreenImage
{
get
{
Bitmap m_BackBitmap = new Bitmap(ScreenSize.Width, ScreenSize.Height);
Graphics _Graphics = Graphics.FromImage(m_BackBitmap);
_Graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), ScreenSize);
_Graphics.Dispose();
return m_BackBitmap;
}
}
/// <summary>
/// 钩子
/// </summary>
private HookMessage m_HookMessage;
/// <summary>
/// 屏幕句柄
/// </summary>
private IntPtr m_ScreenForm;
/// <summary>
/// 图形
/// </summary>
private Bitmap m_Image; public delegate void GetImage(Image p_Image);
/// <summary>
/// 获取屏幕截图
/// </summary>
public event GetImage GetScreenImage; /// <summary>
/// 构造
/// </summary>
public CopyScreen()
{
m_ScreenForm = API.GetDesktopWindow();
m_HookMessage = new HookMessage(API.WindowsHookCodes.WH_MOUSE_LL, true);
m_HookMessage.GetHook += new HookMessage.GetHookMessage(m_HookMessage_GetHook);
} /// <summary>
/// 钩子事件
/// </summary>
/// <param name="p_Code"></param>
/// <param name="p_wParam"></param>
/// <param name="p_lParam"></param>
/// <param name="p_Send"></param>
void m_HookMessage_GetHook(int p_Code, IntPtr p_wParam, IntPtr p_lParam, ref bool p_Send)
{
if (m_StarMouse)
{
switch (p_wParam.ToInt32())
{
case 512: //Move
MouseMove();
break;
case 513: //Down
MouseDown();
p_Send = false;
break;
case 514: //Up
MouseUp();
p_Send = false;
break;
default:
m_StarMouse = false;
break;
}
}
} /// <summary>
/// 根据矩形 如果Width是正直接返回 如果使-会转换成正的矩形 保证大小位置不变
/// </summary>
/// <param name="p_Rectangle">矩形</param>
/// <returns>正矩形</returns>
public static Rectangle GetUprightRectangle(Rectangle p_Rectangle)
{
Rectangle _Rect = p_Rectangle;
if (_Rect.Width < 0)
{
int _X = _Rect.X;
_Rect.X = _Rect.Width + _Rect.X;
_Rect.Width = _X - _Rect.X;
}
if (_Rect.Height < 0)
{
int _Y = _Rect.Y;
_Rect.Y = _Rect.Height + _Rect.Y;
_Rect.Height = _Y - _Rect.Y;
}
return _Rect;
} private Rectangle m_MouseRectangle = new Rectangle(0, 0, 0, 0);
private bool m_DrawStar = false;
private void MouseDown()
{
m_MouseRectangle.X = MousePoint.X;
m_MouseRectangle.Y = MousePoint.Y;
m_DrawStar = true;
}
private void MouseMove()
{
if (m_DrawStar)
{
ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.Transparent, FrameStyle.Dashed);
m_MouseRectangle.Width = MousePoint.X - m_MouseRectangle.X;
m_MouseRectangle.Height = MousePoint.Y - m_MouseRectangle.Y;
ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.White, FrameStyle.Dashed); }
}
private void MouseUp()
{
ControlPaint.DrawReversibleFrame(m_MouseRectangle, Color.Transparent, FrameStyle.Dashed);
m_DrawStar = false;
m_StarMouse = false;
Rectangle _ScreenRectangle = GetUprightRectangle(m_MouseRectangle);
m_MouseRectangle.X = 0;
m_MouseRectangle.Y = 0;
m_MouseRectangle.Width = 0;
m_MouseRectangle.Height = 0;
if (GetScreenImage != null)
{
if (_ScreenRectangle.Width != 0 && _ScreenRectangle.Height != 0) GetScreenImage(m_Image.Clone(_ScreenRectangle, m_Image.PixelFormat));
}
}
private bool m_StarMouse = false; /// <summary>
/// 获取图形
/// </summary>
public void GerScreenFormRectangle()
{
m_Image = ScreenImage;
m_StarMouse = true;
} /// <summary>
/// 获取图形
/// </summary>
public void GetScreen()
{
if (GetScreenImage != null) GetScreenImage(ScreenImage);
} } /// <summary>
/// 用钩子获取消息
/// zgke@Sina.com
/// </summary>
public class HookMessage
{
private IntPtr m_HookEx;
/// <summary>
/// 设置自己进程的钩子
/// </summary>
/// <param name="p_HookCodes">钩子类型</param>
public HookMessage(API.WindowsHookCodes p_HookCodes)
{
m_HookEx = API.SetWindowsHookEx((int)p_HookCodes, new API.HookProc(SetHookProc), IntPtr.Zero, API.GetCurrentThreadId());
}
/// <summary>
/// 设置进程的钩子
/// </summary>
/// <param name="p_HookCodes">钩子类型</param>
/// <param name="p_ThreadID">全局钩子</param>
public HookMessage(API.WindowsHookCodes p_HookCodes, bool p_Zero)
{
IntPtr _Value = System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]);
m_HookEx = API.SetWindowsHookEx((int)p_HookCodes, new API.HookProc(SetHookProc), _Value, 0);
}
/// <summary>
/// 关闭钩子
/// </summary>
public void UnHookMessage()
{
if (API.UnhookWindowsHookEx(m_HookEx))
{
m_HookEx = IntPtr.Zero;
}
} public delegate void GetHookMessage(int p_Code, IntPtr p_wParam, IntPtr p_lParam, ref bool p_Send);
public event GetHookMessage GetHook; private IntPtr SetHookProc(int p_Code, IntPtr p_wParam, IntPtr p_lParam)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
bool _SendMessage = true;
if (GetHook != null) GetHook(p_Code, p_wParam, p_lParam, ref _SendMessage);
if (!_SendMessage) return new IntPtr(1);
return IntPtr.Zero;
}
}
}

在其他项目里引用这个项目就好了

winform实现矩形框截图的更多相关文章

  1. C# GDI绘制矩形框,鼠标左键拖动可移动矩形框,滚轮放大缩小矩形框

    最近工作需要,要做一个矩形框,并且 用鼠标左键拖动矩形框移动其位置.网上查了一些感觉他们做的挺复杂的.我自己研究一天,做了一个比较简单的,发表出来供大家参考一下.如觉得简单,可路过,谢谢.哈哈. 先大 ...

  2. 【python】PIL 批量绘制图片矩形框工具

    工具采用PIL:Python Imaging Library,图像处理标准库.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装 $ ...

  3. 如何用 matlab 在图片上绘制矩形框 和 添加文字 ?

    如何给图像添加矩形框?以及添加想要输入的文字 ? 案例程序,如下所示: clc; close all; clear all;image = imread('/home/wangxiao/Picture ...

  4. Winform 下拉框绑定问题

    在Winform中下拉框绑定的时候只能读到text属性值,Id的值不管怎么搞都读取不到,所以就百度找到了一种方式: public void CmdBind() { var data = _logic. ...

  5. Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整(原理:底层SurfaceView+上层绘制ImageView)

    Android摄像头:只拍摄SurfaceView预览界面特定区域内容(矩形框)---完整实现(原理:底层SurfaceView+上层绘制ImageView) 分类: Android开发 Androi ...

  6. c#在pictureBox控件上绘制多个矩形框及删除绘制的矩形框

    在pictureBox上每次只绘制一个矩形框,绘制下一个矩形框时上次绘制的矩形框取消,代码如链接:https://www.cnblogs.com/luxiao/p/5625196.html 在绘制矩形 ...

  7. WPF 矩形框8个控制点伸缩及拖拽

    最近在研发图片控件矩形框8个控制点进行控制边框的大小.位置等信息,之前查阅了相关的信息,比如别人整合的类:ControlResizer 这个类虽然是好,但是很大程度上是有限制,换句话说,它需要你二次更 ...

  8. Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)

    Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box) function DecideOberlap(BBox_x1, BBox_y1, BBox_x2, ...

  9. GreenOpenPaint的实现(五)矩形框

    想显示一副图片相对来说比较容易.但是想在SDI中显示一个可以拖拽的矩形框,并且在滚动和缩放下都不变形,是很困难的. MFC应该说在这个方面提供了强大的支持,但是也有一些是由于历史的原因需要去除的地方, ...

随机推荐

  1. hdu1034

    #include<stdio.h>const int MAXN=1000;int a[MAXN];int main(){ int n; int i; while(scanf("% ...

  2. [UWP小白日记-3]记账项目-1

    学了一段时间的UWP,来个项目试试手. 本来是想边做边学MVVMLight的结果感觉MVVM对于萌新来说太高难,以后再把这个项目改造成MVVMLight框架的项目. 下面进入正题. 中间那快空白打算放 ...

  3. js的严格模式

    严格模式: 严格模式这下的主要区别如下: 严格模式下的好处:

  4. 前端tab页实例

    <div class="tabbable"> <ul class="nav nav-tabs padding-16"> <c:fo ...

  5. ContourLine

    #define MULTI_PLOT true //Determine whether or not to plot multiple iterations. #define X_MAX 1.0 // ...

  6. hdu 5901 Count primes 素数计数模板

    转自:http://blog.csdn.net/chaiwenjun000/article/details/52589457 计从1到n的素数个数 两个模板 时间复杂度O(n^(3/4)) #incl ...

  7. JS定时器设置、快速取消

    1.首先定义自己的方法 function test() { alert("开始"); } 2.在定时器中使用 setInterval("test()",1000 ...

  8. hdu5514 非2的次幂容斥原理

    /* 1 126 223092870 210 330 390 462 510 546 570 690 714 770 798 858 910 966 1122 1155 1190 1254 1326 ...

  9. When Hybrid Cloud Meets Flash Crowd: Towards Cost-Effective Service Provisioning--INFOCOM 2015

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]

  10. Hadoop实践

    1.将HDFS中的文本文件读取并以JSON格式转存到MongoDB时,报磁盘不足的异常. 实验室的5台计算机的存储空间都在500G以上,就目前存储的数据量来看,完全达不到磁盘接近饱和的状态.通过查看H ...