C# 截取电脑屏幕全屏代码,自适应分辨率。包含labview调用方法。

本文详细介绍了如何使用C# WinApi编程实现电脑屏幕全屏的截取,并展示了如何根据用户输入路径保存截图。通过cdll类的GetScreen方法,程序员可以轻松集成到UI中,配合Form1的按钮操作,实现了隐藏UI后的截图功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

截取电脑屏幕全屏:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.程序集代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace PrintScreen
{
    public class cdll
    {
        #region 使用WinApi获取正确的分辨率
        [DllImport("User32.dll", EntryPoint = "GetDC")]
        private extern static IntPtr GetDC(IntPtr hWnd);

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

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

        public static Bitmap GetScreen(string path="")
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            int w = GetDeviceCaps(hdc, DESKTOPHORZRES);
            int h = GetDeviceCaps(hdc, DESKTOPVERTRES);

            Bitmap bmp = new Bitmap(w, h, PixelFormat.Format32bppRgb);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                //绘制屏幕
                g.CopyFromScreen(0, 0, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
            }
            //判断路径是否为空或者非法
            if (path != "")
            {
                Regex regex = new Regex(@"^([a-zA-Z]:\\)?[^\/\:\*\?\""\<\>\|\,]*$");
                Match m = regex.Match(path);
                if (!m.Success)
                {
                    return bmp;
                }
                string pathroot= System.IO.Path.GetDirectoryName(path);
                if (System.IO.Directory.Exists(pathroot))
                    bmp.Save(path);
                else
                {
                    System.IO.Directory.CreateDirectory(pathroot);
                    bmp.Save(path);
                }
            }         
            return bmp;
        }
    }
}

2.UI代码


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace PrintScreen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
      


      
        private void button1_Click(object sender, EventArgs e)
        {
            if (cbHide.Checked)
            {
                this.Hide();
                //从显示到隐藏需要有个反应时间
                Thread.Sleep(200);
            }

         

            pictureBox1.Image = PrintScreen.cdll.GetScreen(this.textBox1.Text);
            if (cbHide.Checked)
            {
                this.Show();
            }
        }
    }
}

资源链接:包含labview的demo链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值