当前位置: 首页 > news >正文

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

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

  • 引入WINAPI函数
  • 调用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();

调用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;}

使用例子

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

   IntPtr hwnd = GetActiveWindow();Texture2D texture = CaptureWindow(hwnd);

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

   IntPtr hwnd = GetDesktopWindow();Texture2D texture = CaptureWindow(hwnd);
http://www.lryc.cn/news/178931.html

相关文章:

  • 【问题思考总结】CPU怎么访问磁盘?CPU只有32位,最多只能访问4GB的空间吗?
  • UG NX二次开发(C++)-CAM-根据刀具对程序组进行重新分组
  • Unity如何实现TreeView
  • Android widget 小部件使用指南强化版
  • Linux下C语言操作网卡的几个代码实例?特别实用
  • noip2011选择旅馆
  • vue造轮子完整指南--npm组件包开发步骤
  • 28 drf-Vue个人向总结-1
  • 线性代数(七) 矩阵分析
  • myArm 全新七轴桌面型机械臂
  • tomcat乱码解决
  • 【Linux】详解线程第三篇——线程同步和生产消费者模型
  • k8s 安装
  • 红队打靶:THE PLANETS: MERCURY打靶思路详解(vulnhub)
  • 【网络协议】IP
  • Python 布尔类型
  • iOS设备管理器iMazing比iTunes好用吗?有哪些优势
  • Opengl之深度测试
  • 利用ICG-NH2/Amine进行DNA标记1686147-55-6星戈瑞
  • Pyecharts数据可视化
  • 集合-List集合
  • vuex的使用
  • raw图片处理软件:DxO PhotoLab 6 mac中文版支持相机格式
  • ReactPortals传送门
  • 【GDB】 command 命令
  • 1038 统计同成绩学生
  • git报错:Failed to connect to 127.0.0.1 port 1080
  • php eayswoole node axios crypto-js 实现大文件分片上传复盘
  • 《Upload-Labs》01. Pass 1~13
  • v-for中的key