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

C# 自动自定义截图的内容

C# 自动自定义截图的内容

using System;
using System.Drawing;
using System.Runtime.InteropServices;
class Program
{[DllImport("user32.dll")]public static extern IntPtr GetDesktopWindow();[DllImport("user32.dll")]public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);[DllImport("user32.dll")]public static extern IntPtr GetForegroundWindow();[DllImport("user32.dll")]public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);[DllImport("gdi32.dll")]public static extern bool DeleteObject(IntPtr hObject);[DllImport("gdi32.dll")]public static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, IntPtr lpInitData);[DllImport("user32.dll")]public static extern IntPtr GetDC(IntPtr hWnd);const uint PW_CLIENTONLY = 0x00000001;public static Bitmap CaptureActiveWindow(){IntPtr hwnd = GetForegroundWindow(); // 获取当前活动窗口句柄Rectangle rect = new Rectangle();GetWindowRect(hwnd, ref rect); // 获取窗口的大小和位置int width = rect.Width - rect.X;int height = rect.Height - rect.Y;IntPtr hdcScreen = GetDC(hwnd); // 获取屏幕设备上下文IntPtr hdcMemory = CreateDC("DISPLAY", null, null, IntPtr.Zero); // 创建内存设备上下文IntPtr hbitmap = IntPtr.Zero;Graphics gCapture = Graphics.FromHdc(hdcMemory); // 从内存设备上下文创建Graphics对象try{hbitmap = PrintWindow(hwnd, hdcMemory, PW_CLIENTONLY); // 捕捉窗口Bitmap bmp = new Bitmap(width, height, gCapture);Graphics g = Graphics.FromImage(bmp);IntPtr hOldBitmap = SelectObject(hdcMemory, hbitmap);g.CopyFromScreen(rect.Left, rect.Top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);SelectObject(hdcMemory, hOldBitmap);return bmp;}finally{DeleteObject(hbitmap);gCapture.Dispose();ReleaseDC(hwnd, hdcScreen);ReleaseDC(hwnd, hdcMemory);}}[DllImport("gdi32.dll")]public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);[DllImport("user32.dll")]public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
}

使用方法

class Program
{static void Main(string[] args){Bitmap bmp = CaptureActiveWindow();bmp.Save("screenshot.png");}// ... 之前声明的API函数 ...
}
http://www.lryc.cn/news/500294.html

相关文章:

  • Java的Stream流:文件处理、排序与串并行流的全面指南
  • [Maven]下载安装、使用与简介
  • 056 WXML+ WXSS+PHP+LW+校园配送商城微信小程序开发与设计 源码 文档 全套资料
  • Python 在同一/或不同PPT文档之间复制幻灯片
  • C#生成CSR(CertificateSigningRequest)和密钥
  • Docker 安装 Oracle创建表空间并导入数据库
  • elementui table子级tree懒加载bug
  • AI与低代码技术融合:如何加速企业智能化应用开发?
  • 【C#】新建窗体文件,Form、UserControl
  • ansible学习笔记之02command模块与shell模块
  • 在Docker中部署禅道,亲测可用
  • C++(十二)
  • 【数学建模】线性规划问题及Matlab求解
  • 【JavaWeb后端学习笔记】Spring全局异常处理器
  • PT8M2102 触控型 8Bit MCU
  • 4. React 性能优化技巧:如何让你的应用更快
  • pytest中使用conftest做测试前置和参数化
  • Spring Boot 中使用 @Transactional 注解配置事务管理
  • MATLAB 建筑顶面面积计算(95)
  • Linux网络编程之---组播和广播
  • Apache Dolphinscheduler可视化 DAG 工作流任务调度系统
  • docker 部署共享文档ZFile
  • 面试题之JVM
  • 二叉树的深搜(不定期更新。。。。。)
  • WebLLM Chat:无服务器、私密的AI聊天体验
  • C#中的模拟服务器与客户端建立连接
  • 【深度学习】利用Java DL4J 构建和训练医疗影像分析模型
  • application.yml 和 bootstrap.yml
  • 使用uniapp开发小程序场景:在百度地图上调用接口返回的设备相关信息并展示
  • ubuntu22.04 使用可以用的镜像源获取你要的镜像