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

news/2024/10/23 7:17:28/

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.ppmy.cn/news/1130760.html

相关文章

【计算机网络】Linux 内核网络概述

文章目的 了解 Linux 内核网络架构通过网络包过滤器或者防火墙获得使用的 IP 数据包(分组)管理技巧熟悉如何在 Linux 内核级别使用套接字 概述 网络应用程序的开发过去这些年按照指数级增长,这样增加了对系统网络子系统的速度要求和产品化要…

Python 中的 Urljoin 简介

文章目录 介绍Python中的urljoin及其使用方法使用urljoin()方法使用urljoin()模块构建URLs使用urljoin()构建URLs 本篇文章介绍了 Python urljoin 及其使用时的行为。 它还使用不同的示例代码演示了在 Python 中使用 urljoin。 介绍Python中的urljoin及其使用方法 URL通常包含…

AutoAnimate - 无需任何配置,一行代码自动为元素添加优雅的过渡动画,可以搭配 Vue / React 和 Sevelt 使用

这个动画库只要一行代码就可以自动在我们的组件中添过渡动画,为什么这么省事高效呢? AutoAnimate 是一个无需任何配置,自动为我们开发的 Web 项目添加平滑过渡动画的 JavaScript 工具库。AutoAnimate 和之前推荐的一些 js 动画库相比&#x…

Java之SpringCloud Alibaba【六】【Alibaba微服务分布式事务组件—Seata】

一、事务简介 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。 在关系数据库中,一个事务由一组SQL语句组成。 事务应该具有4个属性: 原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。 原子性(atomicity) ∶个事务…

数学建模Matlab之评价类方法

大部分方法来自于http://t.csdnimg.cn/P5zOD 层次分析法 层次分析法(Analytic Hierarchy Process, AHP)是一种结构决策的定量方法,主要用于处理复杂问题的决策分析。它将问题分解为目标、准则和方案等不同层次,通过成对比较和计算…

vue 实现数字验证码功能

需求&#xff1a;写了一个 手机发送验证码后 输入固定验证码的功能 封装成一个组件,如下: <template><div class"conts"><div class"box"><div class"code_list"><div :class"[ code_item, hideIndex 0 ? co…

【Flink】

事件驱动型应用 核心目标&#xff1a;数据流上的有状态计算 Apache Flink是一个框架和分布式处理引擎&#xff0c;用于对无界或有界数据流进行有状态计算。 运行逻辑 状态 把流处理需要的额外数据保存成一个“状态”,然后针对这条数据进行处理,并且更新状态。这就是所谓的“…

孤举者难起,众行者易趋,openGauss 5.1.0版本正式发布!

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…