有了之前的基础知识了解,现在开始实现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
;
}