winform实现矩形框截图

使用方法如下:

 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>
    /// 一个根据矩形截图类
    /// [email protected]
    /// 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>
    /// 用钩子获取消息
    /// [email protected]
    /// </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;
        }
    }
}

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

时间: 2024-10-14 10:12:22

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

实现在矩形框中输入文字,可以显示剩余字数的功能

如下图: 要实现上面的功能,需要做到三点: 1.实现矩形框布局 思路就是矩形框作为整个布局的一个background,在drawable中创建一个shap.xml样式文件 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="r

GreenOpenPaint的实现(五)矩形框

想显示一副图片相对来说比较容易.但是想在SDI中显示一个可以拖拽的矩形框,并且在滚动和缩放下都不变形,是很困难的. MFC应该说在这个方面提供了强大的支持,但是也有一些是由于历史的原因需要去除的地方,特别是在缩放的这个地方还存在一些问题. 我认为关键是形成知识的框架,这样面对未来的需求就能够灵活应变. 这里,我结合GreenOpenPaint的具体实现进行总结. 我总结了一下,有几点: 1.肯定是要为这个矩形生成专门的类.这个类的实现,需要在DOC中,因为DOC就是用来管理所有的模型的:那么注意

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

Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box) 1 function DecideOberlap(BBox_x1, BBox_y1, BBox_x2, BBox_y2, BBox_gt_x1, BBox_gt_y1, BBox_gt_x2, BBox_gt_y2) 2 3 x1 = BBox_x1; 4 y1 = BBox_y1; 5 width1 = BBox_x2 - BBox_x1; 6 height1 = BBox_y2 - BBox_

对opencv MeanShift 融合矩形框的改进

OPENCV 中的代码改进,当然要根据自己的实际情况来,OPENCV 中行人检测有两种矩形框的融合算法,这里只对meanshift 方法做改进 如果有更好的方法,希望可以跟我讲下. 对于去除重合部分,我也写了改进,看懂了可以加到自己程序中. 为什么要做局部MeanShift? 图1.全局MeanShift 如图所示:两幅图像距离较近且有多个矩形框,全局MeanShift融合后可能会造成这种结果 而如果用局部融合就能避免这种情况. /*-------------------------------

winform消息提示框

摘自:http://www.cnblogs.com/risen/archive/2008/01/15/1039751.html public partial class AlertForm : Form    {        /*         * 在类AlertForm定义的下方,         * 我们创建用于显示的字符串和其颜色的变量,         * 再定义几个Rectangle对象的变量用于放置标题.         * 提示内容以及可以拖动窗体的区域和关闭按钮的区域.   

HDU 3265 Posters(线段树扫描线&#183;矩形框面积并)

题意  把一些矩形海报挖去一部分小矩形贴在指定位置  问最后海报覆盖的面积 一个矩形框可以分割成4个独立的小矩形  然后就能用扫描线求面积并了 #include <cstdio> #include <algorithm> using namespace std; const int N = 100005, M = N << 2; typedef long long ll; struct SLine { int x, y1, y2, flag; SLine() {}; S

Winform 下拉框绑定问题

在Winform中下拉框绑定的时候只能读到text属性值,Id的值不管怎么搞都读取不到,所以就百度找到了一种方式: public void CmdBind() { var data = _logic.GetCurrency();//数据源 if (!data.Any()) return; foreach (var currency in data) { cmdCurrencyName.Items.Add(new CurrencyItem { CurrencyName = currency.Des

Skyline实现橡皮筋效果绘制矩形框

这种类似于框选的效果用的比较普遍,一般三维平台和GIS平台都提供了支持接口,可是Skyline就是这么傲娇! 思路是这样的:绘制出的矩形框应该是一直与屏幕边框平行的,也就是矩形框的实际旋转角度是等于摄像机的偏移角YAW值的,这样我们只要知道矩形的宽度,用矩形的两个对角点沿摄像机方向或者沿摄像机反方向延长一个宽度的距离就能得到矩形的另外两个点了.这个宽度利用两个对角点的距离和摄像机偏移角度很容易就能算出来,就不赘叙了. 最核心的代码就是正确计算矩形对角线与水平方向的夹角了: 1 //计算矩形对角点

用canvas实现鼠标拖动绘制矩形框

需要用到jCanvas插件和jQuery. jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas.min.js 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>鼠标拖动绘制矩形框(canvas)</ti