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

news/2025/3/14 1:43:14/
在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.ppmy.cn/news/1049002.html

相关文章

高效进行按数量归类,自动新建文件夹,高效管理文件!

大家好&#xff01;在进行文件管理时&#xff0c;我们经常需要将大量的文件按数量进行归类&#xff0c;以便更好地组织和管理。为了让您能够高效管理文件&#xff0c;我们特别推出了一款智能工具——文件自动归类软件&#xff01;让您能够轻松将文件按数量自动新建文件夹&#…

剪枝基础与实战(1): 概述

本文介绍基于L1正则化的剪枝原理,并以VGG网络进行实战说明。将从零详细介绍模型训练、稀疏化、剪枝、finetune的全过程,提供详细的源码及说明,有助于对剪枝的熟练掌握,后续也会对yolov8进行剪枝的介绍。 论文: Learning Efficient Convolutional Networks through Network …

无则插入有则更新(PostgreSQL,MySQL,Oracle、SqlServer)

无则插入有则更新 PostgreSQL 无则插入有则更新 conflict(带有唯一性约束的字段)&#xff0c;根据此字段判断是更新还是插入 INSERT INTO student(id,name,sex) VALUES(1, 小明, 男) ON conflict (id) DO UPDATE SET id 1,name 小明,sex 男;无则插入有则不做操作 INSERT I…

Java并发三大利器之深度解析

推荐阅读 AI文本 OCR识别最佳实践 AI Gamma一键生成PPT工具直达链接 玩转cloud Studio 在线编码神器 玩转 GPU AI绘画、AI讲话、翻译,GPU点亮AI想象空间 资源分享 「java、python面试题」来自UC网盘app分享&#xff0c;打开手机app&#xff0c;额外获得1T空间 https://dr…

两个字符串的删除操作——力扣583

class Solution {public:int minDistance(string word1, string word2) {int m = word1.length(), n=word2

8月21日,每日信息差

1、多地餐厅“七夕”线上订座量翻倍 2、北京将严禁使用AI生成处方 3、多家上市公司及旗下公司因环境违法受罚 4、香港科技大学发射香港高教界首枚卫星&#xff0c;在甘肃酒泉卫星发射中心发射一颗名为“香港科大-雄彬一号”的多光谱光学卫星&#xff0c;以监测与全球环境、灾…

redis--事务

redis事务 在Redis中&#xff0c;事务是一组原子性操作的集合&#xff0c;它们被一起执行&#xff0c;要么全部执行成功&#xff0c;要么全部回滚。虽然Redis的事务并不遵循传统数据库的ACID特性&#xff0c;但它仍然提供了一种将多个命令打包成一组执行的机制&#xff0c;适用…

SpringCloud全家通新人入门手册

一、架构图 二、springCloud全家桶组件库 三、Spring Cloud 实战项目全景规划 四、技术选型 第一阶段&#xff1a;搭建基础的微服务功能&#xff0c;实现微服务之间的通信&#xff1b; 1、服务治理&#xff1a;服务治理的重点是搭建基础的跨服务调用功能。我会把用户服务、优…