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

.NET Core全屏截图,C#全屏截图

.NET Core全屏截图,C#全屏截图

使用框架:

  • WPF
  • .NET 8
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Runtime.InteropServices;
using System.IO;

namespace WpfAppYolo11_test1.Tool
{
    /// <summary>
    /// 屏幕全屏截图
    /// </summary>
    public class ScrentCapter
    {
        [DllImport("User32.dll", EntryPoint = "GetDC")]
        private extern static IntPtr GetDC(IntPtr hWnd);


        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        const int DESKTOPVERTRES = 117;
        const int DESKTOPHORZRES = 118;

        /// <summary>
        /// 截取电脑全屏图片
        /// </summary>
        public static string Screenshot()
        {
            // 获取屏幕的尺寸
            IntPtr hdc = GetDC(IntPtr.Zero);
            //屏幕宽度
            int width = GetDeviceCaps(hdc, DESKTOPHORZRES);
            //屏幕高度
            int height = GetDeviceCaps(hdc, DESKTOPVERTRES);

            System.Drawing.Size size = new System.Drawing.Size()
            {
                Width = width,
                Height = height
            };
            string filePath = string.Empty;
            // 创建一个Bitmap对象,用于存储屏幕截图
            using (Bitmap bitmap = new Bitmap(width, height))
            {
                // 创建一个Graphics对象,用于绘制到Bitmap上
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // 截取屏幕内容到Graphics对象中
                    g.CopyFromScreen(0, 0, 0, 0, size);
                }

                // 保存Bitmap为图片文件
                string dir = Path.Combine(AppContext.BaseDirectory, "screen");
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                filePath =Path.Combine(dir, $"screen{DateTime.Now.ToString("yyyyMMdd_HHmmssfff")}.png");
                bitmap.Save(filePath, ImageFormat.Png);
            }
            return filePath;
        }
    }
}

相关文章:

  • Windows 如何开启和使用FTP服务
  • 从零开始训练小型语言模型之minimind
  • cannon g3810打印机设置
  • Python自学指南:从入门到进阶(第一天)
  • Mysql的卸载安装配置以及简单使用
  • 【GPT入门】第3课 客服会话质检(思维链)
  • SGLang Router:基于缓存感知负载均衡的数据并行路由实践
  • 约束问题的最优性条件
  • 小迪安全-27-php开发,tp框架,路由访问,对象操作,内置过滤,核心漏洞
  • MoonSharp 文档一
  • 六十天前端强化训练之第十四天之深入理解JavaScript异步编程
  • 硬件小知识之升压电路--1
  • 驱动 AI 边缘计算新时代!高性能 i.MX 95 应用平台引领未来
  • Java面向对象(详细解释)
  • 【Mastering Vim 2_10】第八章:玩转 Vimscript(中)—— Vim9 脚本的基础语法概述
  • BUUCTF——[GYCTF2020]FlaskApp1 SSTI模板注入/PIN学习
  • 深入探讨AI-Ops架构 第一讲 - 运维的进化历程以及未来发展趋势
  • JQuery 语法 $
  • Python中`for`循环的简单使用示例
  • 数据结构链式表
  • 外包网站开发多少钱/国内最开放的浏览器
  • 上海城市建设大学网站/吉林seo网络推广
  • 鲜花网站建设的项目介绍/搜索电影免费观看播放
  • 网站备案名称更改/营销技巧有哪些
  • 网站建设适用税种/电商营销推广方法
  • php培训学校网站源码/seo研究中心南宁线下