zoukankan      html  css  js  c++  java
  • C#使用PrintWindow进行窗口抓图

    本文和C++使用PrintWindow进行窗口抓图对应,使用C#实现。

    Win32封装同C#使用BitBlt进行窗口抓图

    PrintCaptureHelper.cs

    using System;
    
    namespace CaptureSharp
    {
        internal class PrintCaptureHelper
        {
            public IntPtr BitmapPtr => _hBitmap;
            public Win32Types.BitmapInfo BitmapInfo { get; } = new Win32Types.BitmapInfo();
            public Win32Types.Rect WindowRect => _windowRect;
            public Win32Types.Rect ClientRect => _clientRect;
            public int BitmapDataSize => _bmpDataSize;
    
            private IntPtr _hWnd = IntPtr.Zero;
            private IntPtr _hScrDc = IntPtr.Zero;
            private IntPtr _hMemDc = IntPtr.Zero;
            private IntPtr _hBitmap = IntPtr.Zero;
            private IntPtr _hOldBitmap = IntPtr.Zero;
    
            private Win32Types.Rect _windowRect;
            private Win32Types.Rect _clientRect;
            private int _bmpDataSize;
    
            public bool Init(IntPtr handle)
            {
                _hWnd = handle;
    
                //获取窗口大小
                if (!Win32Funcs.GetWindowRect(_hWnd, out _windowRect) ||
                    !Win32Funcs.GetClientRect(_hWnd, out _clientRect))
                {
                    return false;
                }
    
                _bmpDataSize = _windowRect.Width * _windowRect.Height * 3;
    
                _hScrDc = Win32Funcs.GetWindowDC(_hWnd);
                _hBitmap = Win32Funcs.CreateCompatibleBitmap(_hScrDc, _windowRect.Width, _windowRect.Height);
                _hMemDc = Win32Funcs.CreateCompatibleDC(_hScrDc);
                _hOldBitmap = Win32Funcs.SelectObject(_hMemDc, _hBitmap);
    
                return true;
            }
    
            public bool Init(string windowName)
            {
                var handle = Win32Funcs.FindWindow(null, windowName);
                if (handle.Equals(IntPtr.Zero))
                {
                    return false;
                }
    
                return Init(handle);
            }
    
            public void Cleanup()
            {
                if (_hBitmap.Equals(IntPtr.Zero))
                {
                    return;
                }
    
                //删除用过的对象
                Win32Funcs.SelectObject(_hMemDc, _hOldBitmap);
                Win32Funcs.DeleteObject(_hBitmap);
                Win32Funcs.DeleteDC(_hMemDc);
                Win32Funcs.ReleaseDC(_hWnd, _hScrDc);
    
                _hWnd = IntPtr.Zero;
                _hScrDc = IntPtr.Zero;
                _hMemDc = IntPtr.Zero;
                _hBitmap = IntPtr.Zero;
                _hOldBitmap = IntPtr.Zero;
            }
    
            public bool RefreshWindow()
            {
                return ChangeWindowHandle(_hWnd);
            }
    
            public bool ChangeWindowHandle(string windowName)
            {
                Cleanup();
                return Init(windowName);
            }
    
            public bool ChangeWindowHandle(IntPtr handle)
            {
                Cleanup();
                return Init(handle);
            }
    
            public IntPtr Capture()
            {
                if (_hMemDc.Equals(IntPtr.Zero) || _hScrDc.Equals(IntPtr.Zero))
                {
                    return IntPtr.Zero;
                }
    
                var ret = Win32Funcs.PrintWindow(_hWnd, _hMemDc,
                    (uint) Win32Consts.PrintWindowMode.PW_CLIENTONLY |
                    (uint) Win32Consts.PrintWindowMode.PW_RENDERFULLCONTENT);
                return ret ? _hBitmap : IntPtr.Zero;
            }
    
            public bool Capture(out IntPtr bitsPtr, out int bufferSize, out Win32Types.Rect rect)
            {
                bitsPtr = _hBitmap;
                bufferSize = _bmpDataSize;
                rect = _clientRect;
    
                if (_hBitmap.Equals(IntPtr.Zero) || _hMemDc.Equals(IntPtr.Zero) || _hScrDc.Equals(IntPtr.Zero))
                {
                    return false;
                }
    
                var ret = Win32Funcs.PrintWindow(_hWnd, _hMemDc,
                    (uint) Win32Consts.PrintWindowMode.PW_CLIENTONLY |
                    (uint) Win32Consts.PrintWindowMode.PW_RENDERFULLCONTENT);
    
                return ret;
            }
        }
    }
  • 相关阅读:
    016 Spark中关于购物篮的设计,以及优化(两个点)
    015 在Spark中关于groupByKey与reduceByKey的区别
    014 在Spark中完成PV与UV的计算,重在源代码
    013 Spark中的资源调优
    012 Spark在IDEA中打jar包,并在集群上运行(包括local模式,standalone模式,yarn模式的集群运行)
    混淆Android JAR包的方法
    学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐
    Android 使用OpenCV的三种方式(Android Studio)
    OpenCV图片拼接的两种方法
    yuv转opencv中的IplImage
  • 原文地址:https://www.cnblogs.com/xhubobo/p/12789482.html
Copyright © 2011-2022 走看看