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

.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;
        }
    }
}


http://www.kler.cn/a/576795.html

相关文章:

  • C++蓝桥杯基础篇(八)
  • python爬虫系列课程6:js定时器
  • Python SQLite3 保姆级教程:从零开始学数据库操作
  • MYSQL之创建数据库和表
  • 队列相关练习
  • springboot多模块项目中IDEA报错找不到符号
  • 基于51单片机的六路抢答器proteus仿真
  • DeepSeek的安全威胁与国际象棋水平测试
  • 云计算:虚拟化、容器化与云存储技术详解
  • FPGA之USB通信实战:基于FX2芯片的Slave FIFO回环测试详解
  • 【CVPR2025】 EVSSM:用状态空间模型高效去模糊
  • 基于粒子群算法的配电网重构
  • 中性点不接地系统单相接地故障Matlab仿真
  • python 使用flask+sqlalchemy 实现简单数据查询接口
  • 【江协科技STM32】ADC数模转换器-学习笔记
  • MWC2025|5G与AI的深度融合势不可挡,赛思高精度时钟同步为其筑基!
  • 如何在Ubuntu上直接编译Apache Doris
  • 小方摄像头接入本地服务器的方法
  • 深入剖析MyBatis缓存机制:原理、源码与实战指南
  • 深度评测阿里云操作系统控制台:功能全面,体验卓越!