• Unity调用API函数对系统桌面和窗口截图


    Unity3D调用WINAPI函数对系统窗口截图

    引入WINAPI函数

    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    using System.Drawing;
    
      [DllImport("user32.dll")]
      private static extern IntPtr GetDC(IntPtr hwnd);
      [DllImport("user32.dll")]
      private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
      [DllImport("gdi32.dll")]
      private static extern IntPtr CreateCompatibleDC(IntPtr hdc);
      [DllImport("gdi32.dll")]
      private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
      [DllImport("gdi32.dll")]
      private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
      [DllImport("gdi32.dll")]
      private static extern bool BitBlt(IntPtr hDestDC, int xDest, int yDest, int wDest, int hDest, IntPtr hSrcDC, int xSrc, int ySrc, int rop);
      const int SRCCOPY = 0x00CC0020;
      [DllImport("gdi32.dll")]
      public static extern int GetBitmapBits(IntPtr hbmp, int cbBuffer, byte[] lpvBits);
      [DllImport("gdi32.dll")]
      private static extern int DeleteDC(IntPtr hdc);
      [DllImport("user32.dll")]
      public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
      [DllImport("gdi32.dll")]
      public static extern bool DeleteObject(IntPtr hObject);
      [DllImport("user32.dll")]
      private static extern IntPtr GetDesktopWindow();
      [DllImport("user32.dll")]
      private static extern IntPtr GetActiveWindow();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    调用WINAPI函数进行截图

      public static Texture2D CaptureWindow(IntPtr hWnd)
      {
        var hscrdc = GetDC(hWnd);
        var windowRect = new Rectangle();
        GetWindowRect(hWnd, ref windowRect);
        int width = Math.Abs(windowRect.Width - windowRect.X);
        int height = Math.Abs(windowRect.Height - windowRect.Y); 
        
        Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
    
        var hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
        var hmemdc = CreateCompatibleDC(hscrdc);
        SelectObject(hmemdc, hbitmap);
        BitBlt(hmemdc, 0, 0, width, height, hscrdc, 0, 0, SRCCOPY);
    
        // 创建一个字节数组来存储像素数据
        byte[] pixels = new byte[width * height * 4];
    
        // 调用 GetBitmapBits 函数,将像素数据存储在字节数组中
        int result = GetBitmapBits(hbitmap, pixels.Length, pixels);
    
        // 检查结果并处理像素数据
        if (result != 0)
        {
          // 处理像素数据
          texture.LoadRawTextureData(pixels);
          texture.Apply();
        }
        else
        {
          Debug.LogError("Failed to retrieve bitmap bits.");
        }
        DeleteObject(hbitmap);
        ReleaseDC(hWnd, hscrdc);
        DeleteDC(hmemdc);
        return texture;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    使用例子

    调用CaptureWindow函数对当前窗口进行截图

       IntPtr hwnd = GetActiveWindow();
       Texture2D texture = CaptureWindow(hwnd);
    
    • 1
    • 2

    调用CaptureWindow函数对桌面进行截图

       IntPtr hwnd = GetDesktopWindow();
       Texture2D texture = CaptureWindow(hwnd);
    
    • 1
    • 2
  • 相关阅读:
    2022-06-29 数据结构与算法-桶排序、计数排序、基数排序
    在ie浏览器下解决pdfjs插件思源宋体字体部分无法识别问题
    浅谈在操控器类中,为何要通过osgGA::CameraManipulator的逆矩阵改变视点位置
    加载VGG模型进行图像分类
    关于指数加权平均(一阶低通滤波)
    IDEA使用内置database数据库连接mysql报错:javax.net.ssl.SSLHandshakeException
    ROS 工作空间及功能包
    全面整理!机器学习常用的回归预测模型
    数据结构初阶——时间复杂度
    叮,GitHub 到账 550 美元「GitHub 热点速览 v.22.26」
  • 原文地址:https://blog.csdn.net/qq_31042143/article/details/133420988