C# Windows登录界面进行截图,控制鼠标键盘等操作实现(三)

在Windows登录界面进行截图,目前尝试到的以下的截图方法可以截取到图,其它的方式要么卡住,要么截出来是黑屏的,直接上代码:
/// <summary>使用Graphics方式捕获桌面截图(效率低于DesktopCapture,主要用户登录界面截图)</summary>internal class GraphicCapture{private const int Desktopvertres = 117;private const int Desktophorzres = 118;/// <summary>当前最新一帧</summary>private Bitmap _currentFrame;private Rectangle _currentScreenBounds;private Graphics _graphic;private readonly object _screenLock = new object();private Bitmap _originBitmap;private bool _isManualCaptureStop;private PixelFormat _pixelFormat = PixelFormat.Bgra32;private Size DesktopSize{get{IntPtr dc = GraphicCapture.GetDC(IntPtr.Zero);int deviceCaps1 = GraphicCapture.GetDeviceCaps(dc, 117);int deviceCaps2 = GraphicCapture.GetDeviceCaps(dc, 118);GraphicCapture.ReleaseDC(IntPtr.Zero, dc);return new Size(deviceCaps2, deviceCaps1);}}[DllImport("user32.dll")]private static extern IntPtr GetDC(IntPtr ptr);[DllImport("gdi32.dll")]private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);[DllImport("User32.dll")]private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);/// <summary>构造函数</summary>public GraphicCapture(){this._currentScreenBounds = this.GetScreenRectangle(Screen.PrimaryScreen);this._originBitmap = this.CreateBitmap();this._graphic = Graphics.FromImage((Image) this._originBitmap);}private Rectangle GetScreenRectangle(Screen screen){Rectangle bounds = screen.Bounds;return new Rectangle(new Point(bounds.X, bounds.Y), this.DesktopSize);}/// <summary>Bitmap转byte[]</summary>/// <param name="bitmap"></param>/// <returns></returns>private byte[] BitmapToByteArray(Bitmap bitmap){System.Drawing.Imaging.PixelFormat format = this.PixelFormatConverter(this.PixelFormat);Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);BitmapData bitmapdata = bitmap.LockBits(rect, ImageLockMode.ReadOnly, format);int length = Math.Abs(bitmapdata.Stride) * bitmap.Height;byte[] destination = new byte[length];Marshal.Copy(bitmapdata.Scan0, destination, 0, length);bitmap.UnlockBits(bitmapdata);return destination;}private Bitmap CreateBitmap(){System.Drawing.Imaging.PixelFormat pixelFormat = this.PixelFormatConverter(this.PixelFormat);Size desktopSize = this.DesktopSize;int width = desktopSize.Width;desktopSize = this.DesktopSize;int height = desktopSize.Height;int format = (int) pixelFormat;return new Bitmap(width, height, (System.Drawing.Imaging.PixelFormat) format);}private System.Drawing.Imaging.PixelFormat PixelFormatConverter(PixelFormat customPixelFormat){if (true);System.Drawing.Imaging.PixelFormat pixelFormat;if (customPixelFormat != PixelFormat.Bgra32){if (customPixelFormat != PixelFormat.Bgr24)throw new ArgumentOutOfRangeException("PixelFormat", (object) this.PixelFormat, (string) null);pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;}elsepixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;if (true);return pixelFormat;}/// <summary>检索最新的桌面图像和关联的元数据</summary>private byte[] GetLatestFrameToByte() => this.BitmapToByteArray(this.GetLatestFrameToBitmap());/// <summary>检索最新的桌面图像</summary>/// <returns></returns>private Bitmap GetLatestFrameToBitmap(){lock (this._screenLock){Graphics graphic = this._graphic;int x = this._currentScreenBounds.X;int y = this._currentScreenBounds.Y;Size desktopSize = this.DesktopSize;int width1 = desktopSize.Width;desktopSize = this.DesktopSize;int height1 = desktopSize.Height;Size blockRegionSize = new Size(width1, height1);graphic.CopyFromScreen(x, y, 0, 0, blockRegionSize);Size newSize;ref Size local = ref newSize;desktopSize = this.DesktopSize;int width2 = (int) ((double) desktopSize.Width * this.Scale);desktopSize = this.DesktopSize;int height2 = (int) ((double) desktopSize.Height * this.Scale);local = new Size(width2, height2);this._currentFrame = newSize.Width == this._originBitmap.Width && newSize.Height == this._originBitmap.Height ? this._originBitmap : new Bitmap((Image) this._originBitmap, newSize);}return this._currentFrame;}/// <summary>捕获图像像素格式,默认为BGRA32</summary>public PixelFormat PixelFormat{get => this._pixelFormat;set{this._pixelFormat = value;this._originBitmap?.Dispose();this._graphic?.Dispose();this._originBitmap = this.CreateBitmap();this._graphic = Graphics.FromImage((Image) this._originBitmap);}}/// <summary>捕获图像缩放大小,默认为1.0</summary>public double Scale { get; set; } = 1.0;/// <summary>新帧捕获事件</summary>public event EventHandler<CaptureFrame> FrameArrived;/// <summary>开始捕获</summary>public void StartCapture(){this._isManualCaptureStop = false;}/// <summary>停止捕获</summary>public void StopCapture(){this._cancellationTokenSource?.Cancel();this._isManualCaptureStop = true;}/// <summary>获取下一帧图像数据</summary>/// <param name="captureFrame"></param>/// <returns></returns>public bool TryGetNextFrame(out CaptureFrame captureFrame){captureFrame = (CaptureFrame) null;if (this._isManualCaptureStop)return false;try{byte[] byteArray = this.BitmapToByteArray(this.GetLatestFrameToBitmap());Size size = new Size((int) ((double) this.DesktopSize.Width * this.Scale), (int) ((double) this.DesktopSize.Height * this.Scale));captureFrame = new CaptureFrame(size, this.PixelFormat, byteArray);return true;}catch (Exception ex){return false;}}}

操作鼠标键盘等操作可以参考本人另一篇文章:使用C#制作可以录制自动化执行Windows操作脚本工具——类似于按键精灵 - log9527 - 博客园 (cnblogs.com)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/46395.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQLite的命令用法

学习数据库直达网站 https://www.runoob.com/sqlite/sqlite-tutorial.html&#xff08;菜鸟教程&#xff09; 这里只分享&#xff0c;基础操作&#xff0c;数据库创建打开……等等 用到查菜鸟教程即可 文章目录 学习数据库直达网站创建一个数据库方式1方式2 创建一个表格插入一…

智能监控系统的守护者:人工智能行为识别技术的崛起与发展

人工智能助力监控系统&#xff1a;行为识别在安全监控中的应用与挑战 摘要&#xff1a; 随着人工智能技术的快速发展&#xff0c;行为识别在监控系统中的应用逐渐成为安全监控领域的重要工具。本文将详细探讨人工智能行为识别技术在监控系统中的应用&#xff0c;以及在实际应用…

乖宝宠物上市,能否打破外资承包中国宠物口粮的现实

近日&#xff0c;乖宝宠物上市了&#xff0c;这是中国宠物行业成功挂牌的第三家公司。同时&#xff0c;昨日&#xff0c;宠物行业最大的盛事“亚洲宠物展”时隔3年&#xff0c;于昨日在上海成功回归。 这两件事情的叠加可谓是双喜临门&#xff0c;行业能够走到今天实属不易&…

报道 | 9月国际运筹优化会议汇总

封面图来源&#xff1a; https://www.pexels.com/zh-cn/photo/1181406/ 九月召开会议汇总&#xff1a; The 96th meeting of the EURO Working Group on Multiple Criteria Decision Aiding (EWG-MCDA) Location: Paris, France Important dates: Conference: September 202…

STP生成树协议

一、STP是什么 STP协议概述生成树协议(spanning tree protocol)&#xff0c;是一种工作在OSI网络模型中第二层(数据链路层)的通信协议&#xff0c;是一种由交换机运行的&#xff0c;基本应用是防止交换机冗余链路产生的环路&#xff0c;用于确保以太网中无环路的逻辑拓扑结构&…

计算机网络-物理层(三)-信道的极限容量

计算机网络-物理层(三)-信道的极限容量 当信号在信道中传输失真不严重时&#xff0c;在信道的输出端&#xff0c;这些信号可以被识别 当信号在信道中&#xff0c;传输失真严重时&#xff0c;在信道的输出端就难以识别 造成失真的因素 码元传输速率信号传输距离噪声干扰传输媒…

CSS中的transform属性有哪些值?并分别描述它们的作用。

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ translate()⭐ rotate()⭐ scale()⭐ skew()⭐ matrix()⭐ scaleX() 和 scaleY()⭐ rotateX()、rotateY() 和 rotateZ()⭐ translateX() 和 translateY()⭐ skewX() 和 skewY()⭐ perspective()⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&…

Python 处理 Excel 表格的 14 个常用操作

目录 1. 安装依赖库 2. 导入库 3. 读取Excel文件 4. 写入Excel文件 5. 创建工作表 6. 访问工作表 7. 读取单元格数据 8. 写入单元格数据 9. 获取行数和列数 10. 过滤数据 11. 排序数据 12. 添加新行 13. 删除行或列 14. 计算汇总统计 总结 无论是数据分析师、财…

MySQL— 基础语法大全及操作演示!!!(下)

MySQL—— 基础语法大全及操作演示&#xff08;下&#xff09;—— 持续更新 三、函数3.1 字符串函数3.2 数值函数3.3 日期函数3.4 流程函数 四、约束4.1 概述4.2 约束演示4.3 外键约束4.3.1 介绍4.3.2 语法4.3.3 删除/更新行为 五、多表查询5.1 多表关系5.1.1 一对多5.1.2 多对…

Matplotlib数据可视化(一)

目录 1.Matplotlib简介 2.Matplotlib绘图基础 2.1 创建画布与子图 2.2 添加画布属性 2.3 绘图的保存与显示 1.Matplotlib简介 Matplotlib是一个用于绘制数据可视化图表的Python库。它提供了广泛的功能和灵活性&#xff0c;可以创建各种类型的图表&#xff0c;包括折线图、…

min-height到底是什么?

1、概念 给元素设置最小高度&#xff0c;当height小于 min-height &#xff0c;min-height会覆盖height的值 2、案例 如果我有一个盒子A&#xff0c;A设置了min-height的高度为200px&#xff1b;并设置了overflow&#xff1a;auto&#xff0c;那么如果里面的内容超过了200px…

Flutter混合项目构建问题集锦

记录一次AS偶发异常导致的项目构建失败问题 以下是报错信息 Could not determine the dependencies of task ‘:app:mergeCeshiDebugAssets’. Could not resolve all task dependencies for configuration ‘:app:CeshiDebugRuntimeClasspath’. Could not resolve project :…

深入理解python虚拟机:程序执行的载体——栈帧

栈帧&#xff08;Stack Frame&#xff09;是 Python 虚拟机中程序执行的载体之一&#xff0c;也是 Python 中的一种执行上下文。每当 Python 执行一个函数或方法时&#xff0c;都会创建一个栈帧来表示当前的函数调用&#xff0c;并将其压入一个称为调用栈&#xff08;Call Stac…

Programming abstractions in C阅读笔记: p118-p122

《Programming Abstractions In C》学习第49天&#xff0c;p118-p122&#xff0c;总结如下&#xff1a; 一、技术总结 1.随机数 (1)seed p119&#xff0c;“The initial value–the value that is used to get the entire process start–is call a seed for the random ge…

LLM 落地电商行业的最佳实践来了?Zilliz X AWS 有话说

目录 01.活动议程 02.时间地点 03.议题介绍 主题一:《电商场景构建 GenAI 应用实践分享》 主题二:《向量数据库在电商及大模型领域应用方案介绍》 <

MySQL 主从复制遇到 1590 报错

作者通过一个主从复制过程中 1590 的错误&#xff0c;说明了 MySQL 8.0 在创建用户授权过程中的注意事项。 作者&#xff1a;王祥 爱可生 DBA 团队成员&#xff0c;主要负责 MySQL 故障处理和性能优化。对技术执着&#xff0c;为客户负责。 本文来源&#xff1a;原创投稿 爱可生…

使用Qt框架开发的2D/3D图像数据标注工具:从设计到实现的探索

随着人工智能的不断发展,数据准备变得越来越关键。尤其在图像处理和计算机视觉领域,高质量的数据标注是关键。今天,我将详细介绍我使用Qt框架开发的一款2D/3D图像数据标注工具,帮助用户更轻松、准确地标注图像数据。 一、背景与挑战 在图像处理和计算机视觉的应用中,如目…

Git常用操作与命令

可以先阅读Git的初次使用。 获取Git仓库 1.在已存在目录中初始化仓库 Git第一步&#xff1a;git init。该命令是在一个目录中创建一个新的Git仓库。 2.克隆现有的仓库 # 例如 git clone https://github.com/vuejs/vue $ git clone <url># 自定义本地仓库的名字 $ git …

若依微服务集成CAS,实现单点登录

若依&#xff08;RuoYi&#xff09;微服务是一款基于Spring Cloud Alibaba开发的企业级微服务框架&#xff0c;采用前后端分离方式&#xff0c;使用了常用的微服务组件&#xff0c;如Feign、Nacos、Sentinel、Seata等&#xff0c;提供了丰富的微服务治理功能&#xff0c;如服务…

Linux查看命令总结

1.动态实时查找命令 使用以下命令的前提是需要在找到日志位置 tail -f server.log 实时展示日志末尾内容&#xff0c;默认最后10行,相当于增加参数 -n 10 tail -n filename; tail命令扩展 查看日志最后20行内容并实时更新日志 tail -f -n 20 server.log或者 tail -fn 20 ser…