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 方法。
最后发图不发种:
C#模拟PrtScn实现截屏的更多相关文章
- iOS中正确的截屏姿势
昨天写了个用到截屏功能的插件,结果问题不断,今天终于解决好了,把debug过程中所有尝试过的截屏方法都贴出来吧- 第一种 这是iOS 3时代开始就被使用的方法,它被废止于iOS 7.iOS的私有方法, ...
- c# wpf 利用截屏键实现截屏功能
原文:c# wpf 利用截屏键实现截屏功能 最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下. 先是找到了这样一份代码: static class Scr ...
- Stick hero "攻略", android 代码编写与分析(后台截屏, 后台模拟点击)
论文写完,感觉头脑好久没被灵感刺激了,前些天室友介绍了个小游戏,我突然来了灵感可以写的简单的android 程序实现自动运行.主要的过会为三步: 1,Android 屏幕的获取.因为安全的原因,过程比 ...
- VB用API模拟截屏键PrintScreen
很多人用 SendKeys "{PRTSC}" 模拟截屏键 PrintScreen 的时候提示<错误:'70' 拒绝的权限>,于是经常遇到人问...干脆写下来 '声明 ...
- iOS检测用户截屏并获取所截图片
iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...
- nodejs+phantomjs+七牛 实现截屏操作并上传七牛存储
近来研究了下phantomjs,只是初涉,还谈不上深入研究,首先介绍下什么是phantomjs. 官网上的介绍是:”PhantomJS is a headless WebKit scriptable ...
- iOS截屏并修改截图然后分享的功能实现
一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScr ...
- Android 长截屏原理
https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/ a ...
- Android8.1 MTK平台 截屏功能分析
前言 涉及到的源码有 frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java vend ...
随机推荐
- JDBC上关于数据库中多表操作一对多关系和多对多关系的实现方法
黑马程序员 我们知道,在设计一个Javabean的时候,要把这些BEAN 的数据存放在数据库中的表结构,然而这些数据库中的表直接又有些特殊的关系,例如员工与部门直接有一对多的关系,学生与老师直接又多对 ...
- Andorid之ActivityManager
在Android中ActivityManager主要用于和系统中运行的Activities进行交互.在本篇文章中,我们将对ActivityManager中的API进行研究使用. 在ActivityMa ...
- 遇到问题描述:Android Please ensure that adb is correctly located at问题解决
遇到问题描述: 运行android程序控制台输出 [2013-11-04 16:18:26 - ] The connection to adb is down, and a severe error ...
- ListView 控件与 内容
1)由控件获取内容:ListViewItem item = Utilities.GetVisualParent<ListViewItem>(chx); if (item == null) ...
- angularJs的一次性数据绑定:双冒号::
angularJs 中双冒号 ::来实现一次性数据绑定. 原文: https://blog.csdn.net/qianxing111/article/details/79971544 -------- ...
- 使用gitolite进行git服务器搭建
使用gitolite进行git服务器搭建 https://blog.csdn.net/pan0755/article/details/78460941 使用gitolite搭建,然后需要有个客户端进行 ...
- 【转】gcc选项
http://zodiac1111.github.io/blog/config-gcc-warning/
- [Algorithm] Serialize and Deserialize Binary Tree
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, ...
- [Backbone]8. Level 7: Router and history
1. Ceate a route Class var AppRouter = Backbone.Router.extend({ }); 2. Add a route name it "sho ...
- 社区类 App 如何引导用户发帖和产生内容?
作者:Pmer在路上链接:http://www.zhihu.com/question/25502904/answer/31342246来源:知乎著作权归作者所有,转载请联系作者获得授权. ugc的产出 ...