C# 模拟PrintScreen 和 Alt+PrintScreen截取屏幕图片

简介: C# 模拟PrintScreen 和 Alt+PrintScreen截取屏幕图片keybd_event API函数功能:该函数合成一次击键事件。系统可使用这种合成的击键事件来产生WM_KEYUP或WM_KEYDOWN消息,键盘驱动程序的中断处理程序调用keybd_event函数。

C# 模拟PrintScreen 和 Alt+PrintScreen截取屏幕图片

keybd_event API

函数功能:该函数合成一次击键事件。系统可使用这种合成的击键事件来产生WM_KEYUP或WM_KEYDOWN消息,键盘 驱动程序中断处理程序调用keybd_event函数。在Windows NT中该函数己被使用SendInput来替代它。
函数原型;VOID keybd_event(BYTE bVk,BYTE bScan,DWORD dwFlags,DWORD dwExtralnfo);
参数:
bVk:定义一个虚拟键码。键码值必须在1~254之间。
  bScan:定义该键的硬件扫描码。
  dwFlags:定义函数操作的各个方面的一个标志位集。应用程序可使用如下一些预定义常数的组合设置标志位。
  KEYEVENTF_EXTENDEDKEY:若指定该值,则扫描码前一个值为OXEO(224)的前缀字节。 
  KEYEVENTF_KEYUP:若指定该值,该键将被释放;若未指定该值,该键将被按下。
  dwExtralnfo:定义与击键相关的附加的32位值。
  返回值:该函数无返回值。

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PrintScreen
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll")]
        static extern void keybd_event
        (
            byte bVk,// 虚拟键值  
            byte bScan,// 硬件扫描码  
            uint dwFlags,// 动作标识  
            IntPtr dwExtraInfo// 与键盘动作关联的辅加信息  
        );

        /// <summary>
        /// 模拟Print Screen键盘消息,截取全屏图片。
        /// </summary>
        public void PrintScreen()
        {
            keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down
            Application.DoEvents(); 
            keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up
            Application.DoEvents(); 
        }

        /// <summary>
        /// 模拟Alt Print Screen键盘消息,截取当前窗口图片。
        /// </summary>
        public void AltPrintScreen()
        {
            keybd_event((byte)Keys.Menu, 0, 0x0, IntPtr.Zero);
            keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down
            Application.DoEvents();
            Application.DoEvents();
            keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up
            keybd_event((byte)Keys.Menu, 0, 0x2, IntPtr.Zero);
            Application.DoEvents();
            Application.DoEvents();
        } 

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 从剪贴板获取图片
        /// </summary>
        /// <returns></returns>
        private Bitmap GetScreenImage()
        {
            IDataObject newobject = null;
            Bitmap NewBitmap = null;

            try
            {
                Application.DoEvents();
                newobject = Clipboard.GetDataObject();

                if (Clipboard.ContainsImage())
                {
                    NewBitmap = (Bitmap)(Clipboard.GetImage().Clone());
                }

                return NewBitmap;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            pictureBox1.Image = null;
            PrintScreen();
            pictureBox1.Image = GetScreenImage();
            button1.Enabled = true;
            Application.DoEvents();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            pictureBox1.Image = null;
            AltPrintScreen();
            pictureBox1.Image = GetScreenImage();
            button2.Enabled = true;
            Application.DoEvents();
        }

    }
}

运行效果:


遗留问题:

PrintScreen没有任务问题,但使用AltPrintScreen时第一次总是不能得到正确的图片,不知道是为什么!希望高手路过指点一二,小弟不胜感激!

目录
相关文章
|
测试技术 C# Windows
C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试
原文:C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试 WPF 没有用到 PictureBox, 而是用Image代替. 下面我试着加载显示一个图片 。 XAML CS Attempt 1: ImageMy_Image=newImage(Openfile.
1765 0
|
30天前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
6月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
260 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
4月前
|
API C#
C# 调用系统“API“设置图片为“桌面壁纸“
C# 调用系统“API“设置图片为“桌面壁纸“
|
6月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
21 0
|
9月前
|
人工智能 文字识别 API
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
120 0
|
区块链 C#
C#实现把图片转换为ico格式
C#实现把图片转换为ico格式
592 0
|
C# 图形学
C#裁剪图片的方法
C#裁剪图片的方法
255 0
如何在 C#中的listView 控件中显示图片?
如何在 C#中的listView 控件中显示图片?
959 0
如何在 C#中的listView 控件中显示图片?