zoukankan      html  css  js  c++  java
  • C#模拟PrtScn实现截屏

    有了之前的基础知识了解,如今開始实现PrtScn和Alt+PrtScn。

    首先新建一个WPF应用程序,命名为PrintscreenAndAltPrintScreen

    导入keybd_event方法
    须要为DllImport加入using System.Runtime.InteropServices;

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

    编写PrtScn函数:

    public void PrintScreen()
    {
        keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down
        System.Windows.Forms.Application.DoEvents(); //加入引用system.windows.forms
        keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up
        System.Windows.Forms.Application.DoEvents();
    }

    编写Alt+PrtScn函数:

    public void AltPrintScreen()
    {
        keybd_event((byte)Keys.Menu, 0, 0x0, IntPtr.Zero);
        keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down
        System.Windows.Forms.Application.DoEvents();
        System.Windows.Forms.Application.DoEvents();
        keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up
        keybd_event((byte)Keys.Menu, 0, 0x2, IntPtr.Zero);
        System.Windows.Forms.Application.DoEvents();
        System.Windows.Forms.Application.DoEvents();
    }

    编写从剪贴板获得图片函数:
    须要加入using System.Drawing;加入引用System.Drawing

    private Bitmap GetScreenImage()
    {
        System.Windows.Forms.IDataObject newobject = null;
        Bitmap NewBitmap = null;
        try
        {
            System.Windows.Forms.Application.DoEvents();
            newobject = System.Windows.Forms.Clipboard.GetDataObject();
            if (System.Windows.Forms.Clipboard.ContainsImage())
            {
                NewBitmap = (Bitmap)(System.Windows.Forms.Clipboard.GetImage().Clone());
            }
            return NewBitmap;
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }
    }

    编写xmal程序:
    简单加入两个Image控件和两个button按钮

     <Window x:Class="PrintscreenAndAltPrintScreen.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="768" Width="1024">
         <Grid> 
             <Image Name="image1" Margin="45,30,562,203"> 
             </Image> 
             <Button Content="PrtScn" Height="24" HorizontalAlignment="Left" Margin="182,552,0,0" Name="button1" VerticalAlignment="Top" Width="95" Click="button1_Click" /> 
             <Button Content="Alt+PrtScn" Height="24" HorizontalAlignment="Left" Margin="718,552,0,0" Name="button2" VerticalAlignment="Top" Width="95" Click="button2_Click" /> 
             <Image Margin="566,30,41,213" Name="image2" /> 
         </Grid> 
     </Window>

    对两个按钮加入click事件:
    这里须要把Bitmap转为BitmapSource。能够參阅博客:WPF(C#)中Bitmap与BitmapImage相互转换

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        image2.Source = null;
        AltPrintScreen();
        Bitmap bitmap = GetScreenImage();
        IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象
    //Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针。返回一个托管的BitmapSource
        BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        image2.Source = bitmapSource;
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        image1.Source = null;
        PrintScreen();
        Bitmap bitmap = GetScreenImage();
        IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象
    //Imaging.CreateBitmapSourceFromHBitmap方法//基于所提供的非托管位图和调色板信息的指针,返回一个托管的BitmapSource
        BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        image1.Source = bitmapSource;
    }

    最后对DoEvents 方法的一点解释:
    Application.DoEvents 方法
    处理当前在消息队列中的全部 Windows 消息。
    语法:

    public static void DoEvents()

    当执行 Windows 窗口时。它将创建新窗口。然后该窗口等待处理事件。该窗口在每次处理事件时。均将处理与该事件关联的全部代码。

    全部其它事件在队列中等待。

    当代码处理事件时。应用程序不会响应。

    比如,假设将甲窗口拖到乙窗口之上,则乙窗口不会又一次绘制。


    假设在代码中调用 DoEvents。则您的应用程序能够处理其它事件。

    比如,假设您有向 ListBox 加入数据的窗口,并将 DoEvents 加入到代码中,那么当将还有一窗口拖到您的窗口上时,该窗口将又一次绘制。

    假设从代码中移除 DoEvents,那么在按钮的单击事件处理程序执行结束曾经。您的窗口不会又一次绘制。
    与 Visual Basic 6.0 不同。 DoEvents 方法不调用 Thread.Sleep 方法。

    最后发图不发种:
    这里写图片描写叙述

  • 相关阅读:
    Android 使用Application总结
    android数据保存
    Android 利用Application对象存取公共数据
    android 通过post方式提交数据的最简便有效的方法
    android http协议post请求方式
    maven下载及配置
    普通的101键盘在Mac上的键位对应
    高效使用你的Xcode
    maven安装及maven项目导入流程
    Mac键盘图标与对应快捷按键标志汇总
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7350917.html
Copyright © 2011-2022 走看看