c# wpf 利用截屏键实现截屏功能
最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下。
先是找到了这样一份代码:
static class ScreenCut
{
public static System.Drawing.Bitmap GetScreenSnapshot()
{
System.Drawing.Rectangle rc = System.Windows.Forms.SystemInformation.VirtualScreen;
System.Drawing.Bitmap bitmap =
new System.Drawing.Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, System.Drawing.CopyPixelOperation.SourceCopy);
}
return bitmap;
}
public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap bmp)
{
BitmapSource returnSource;
try
{
returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
returnSource = null;
}
return returnSource;
}
public static BitmapSource CopyFromScreenSnapshot(BitmapSource screenSnapshot,
Rect region)
{
var sourceRect =
new System.Drawing.Rectangle((int)region.Left, (int)region.Top, (int)region.Width, (int)region.Height);
var destRect =
new System.Drawing.Rectangle(0, 0, sourceRect.Width, sourceRect.Height);
if (screenSnapshot !=
null)
{
var bitmap =
new System.Drawing.Bitmap(sourceRect.Width, sourceRect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
g.DrawImage(bitmap, destRect, sourceRect.Left, sourceRect.Top, sourceRect.Width, sourceRect.Height, System.Drawing.GraphicsUnit.Pixel);
}
return bitmap.ToBitmapSource();
}
return null;
}
}
调用时包装一下,下面的函数截下屏幕指定区域并保存:
System.Drawing.Image takePicture(Rect wnd,
String saveLocation,
String pictureName)
{
BitmapSource bitmapScr =
ScreenCut.CopyFromScreenSnapshot(ScreenCut.ToBitmapSource(ScreenCut.GetScreenSnapshot()),wnd);
PngBitmapEncoder encoder =
new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapScr));
FileStream fileStream =
new FileStream(saveLocation +
"\\" + pictureName ".png",
FileMode.Create, FileAccess.Write);
encoder.Save(fileStream);
fileStream.Close();
}
但实际效果保存下来的图是空的,经验证前两个函数没有问题。于是我查资料找到了另一种切割BitmapSource的方法,将第三个函数改成这样:
BitmapSource CopyFromScreenSnapshot(BitmapSource screenSnapshot,
Rect region)
{
Int32Rect window = new
Int32Rect(region.X, region.Y, region.Width, region.Height);
BitmapSource bitmapScr =
ScreenCut.ToBitmapSource(ScreenCut.GetScreenSnapshot());
//计算Stride
int stride = bitmapScr.Format.BitsPerPixel * window.Width / 8;
//声明字节数组
byte[] data =
new byte[window.Height * stride];
//调用CopyPixels
bitmapScr.CopyPixels(window, data, stride, 0);
PngBitmapEncoder encoder =
new PngBitmapEncoder();
return BitmapSource.Create(window.Width, window.Height, 0, 0,
PixelFormats.Bgr32,
null, data, stride);
}
这样总算能截到屏了。不过后来发现在某些主题下有些程序的窗口在截下的图中是透明的。这个方法还是不够完美。
我想到了利用键盘的截屏键截图再进行剪切的方法。分为三步:1、模拟按下截屏键 2、从剪贴板获取图片 3、截取和保存图片。
对于第一点,利用SendMessage函数发送WM_KEY事件没有用,不过我找到了另一个模拟键盘输入的方法:
using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static extern void keybd_event
(
byte bVk,// 虚拟键值
byte bScan,// 硬件扫描码
uint dwFlags,// 动作标识
IntPtr dwExtraInfo// 与键盘动作关联的辅加信息
);
上面这个函数对本程序发送按钮事件,模拟截屏键的话像下面这样写,wpf需要在项目添加引用System.Windows.Forms
const int VK_SNAPSHOT = 0x2C;
public void PrintScreen()
{
keybd_event((byte)VK_SNAPSHOT, 0, 0x0,
IntPtr.Zero);//down
System.Windows.Forms.Application.DoEvents();//强制窗口响应按钮事件
keybd_event((byte)VK_SNAPSHOT, 0, 0x2,
IntPtr.Zero);//up
System.Windows.Forms.Application.DoEvents();
}
接下来从剪贴板获取图片,wpf需要在项目添加引用System.Windows.Forms System.Drawing
if (System.Windows.Forms.Clipboard.ContainsImage())
{
System.Drawing.Image image = System.Windows.Forms.Clipboard.GetImage();
}
然后剪切。
System.Drawing.Image cutPicture(System.Drawing.Image image,
Int32Rect window)
{
PrintScreen();
if (window.X + window.Width > image.Width)
window.Width = image.Width - window.X;
if (window.Y + window.Height > image.Height)
window.Height = image.Height - window.Y;
if (image ==
null)
{
//新建一个bmp图片
System.Drawing.Image bitmap =
new System.Drawing.Bitmap(window.Width, window.Height);
//新建一个画板
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量查值法
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
graphic.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
graphic.DrawImage(image, new System.Drawing.Rectangle(0, 0, window.Width, window.Height),
new System.Drawing.Rectangle(window.X, window.Y, window.Width, window.Height), System.Drawing.GraphicsUnit.Pixel);
return bitmap;
}
else
{
return null;
}
}
最后包装一下
System.Drawing.Image cutScreen(Int32Rect window)
{
PrintScreen();
Thread.Sleep(1000);
if (System.Windows.Forms.Clipboard.ContainsImage())
{
System.Drawing.Image image = cutPicture(System.Windows.Forms.Clipboard.GetImage(),
Int32Rect window);
return image;
}
else
{
Console.WriteLine("clipboard doesn't contain picture");
return null;
}
}
System.Drawing.Image类有实例函数Save,保存很简单,就不细说了。
资料来源:
http://www.cnblogs.com/zhouyinhui/archive/2010/08/20/1804762.html
https://www.mgenware.com/blog/?p=285
http://blog.csdn.net/testcs_dn/article/details/39762017
c# wpf 利用截屏键实现截屏功能的更多相关文章
- VB用API模拟截屏键PrintScreen
很多人用 SendKeys "{PRTSC}" 模拟截屏键 PrintScreen 的时候提示<错误:'70' 拒绝的权限>,于是经常遇到人问...干脆写下来 '声明 ...
- Android滚动截屏,ScrollView截屏
在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...
- C#在截屏时将截屏之前需要隐藏的控件也截入
最近我在项目中遇到一个让我十分头疼的问题,就是我在截屏时也将截屏之前隐藏的控件也截入了. 情况:我在Winform窗体有个截屏功能按钮,实现在调用WPF全屏后截屏,但在截屏WPF界面前将界面里的一个L ...
- iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)
iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏) 2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2 1. 截取屏幕尺寸大小的图片并保存至相册 ...
- WPF利用动画实现圆形进度条
原文:WPF利用动画实现圆形进度条 这是我的第一篇随笔,最近因为工作需要,开始学习WPF相关技术,自己想实现以下圆形进度条的效果,逛了园子发现基本都是很久以前的文章,实现方式一般都是GDI实现的,想到 ...
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
WPF利用通过父控件属性来获得绑定数据源RelativeSource 有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中 ...
- WPF利用radiobutton制作菜单按钮
原文:WPF利用radiobutton制作菜单按钮 版权声明:欢迎转载.转载请注明出处,谢谢 https://blog.csdn.net/wzcool273509239/article/details ...
- WPF利用HelixToolKit后台导入3D模型
原文:WPF利用HelixToolKit后台导入3D模型 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/de ...
- WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象
原文:WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象 简介 本文将完整叙述我利用VisualTreeHelper实现题述功能的全部过程,想直接看函数实现的朋友可以跳到函数 ...
随机推荐
- android.app.Dialog(23)里window的那些事(坑)
不要使用theme去配置Dialog的gravity 因为如今手机的尺寸比較大(相对于智能机開始的3.5in.4.0in),而Dialog默认都是显示在屏幕中心的位置,用户触摸起来多不便. 所以大多数 ...
- Java解析HTML之HTMLParser使用与详解 分类: C_OHTERS 2014-05-19 21:46 2309人阅读 评论(0) 收藏
转自:http://free0007.iteye.com/blog/1131163 HTMLParser具有小巧,快速的优点,缺点是相关文档比较少(英文的也少),很多功能需要自己摸索.对于初学者还是要 ...
- Struts2与Spring的整合
今天倒腾了半天,终于是把这个两个框架整合到一起了.还是要写一下总结,同时给大家一些帮助. 开发环境:myeclipse 9.0(不好用!)tomcat6.0 1.准备工作 需要导入的包:struts2 ...
- QT学习记录之环境搭建
作者:朱金灿 来源:http://blog.csdn.net/clever101 1. 安装qt-win-opensource-4.8.5-vs2008.exe(对应的IDE是VS2008),安装路径 ...
- Linux非图形界面安装程序
安装Linux程序的时候一般不会调取图形界面,这样输错内容,返回上一步时需要使用命令 previous ,相应的exit 与 next..在中文操作的时候,不会提示,所以要记住 在安装过程中,我们可能 ...
- NSString与int和float的相互转换
NSString *tempA = @"123"; NSString *tempB = @"456"; 1,字符串拼接 NSString *newString ...
- 解析字典包含关键字比如ID,description等,MJExtension 框架 不能直接设置变量与其同名。
@property (nonatomic,strong) NSString *descrip; //设置变量名 _DataReceived=(NSMutableArray *)[HZnewsmess ...
- WIN内核线程池函数
线程池 (本章节中样例都是用 VS2010 编译调试的) 线程池编写必须在 Windows Vista 操作系统(以及以上版本号的操作系统)下,且 C++ 编译器版本号至少是 VS2008 线程池的功 ...
- 【9210】找礼物(char* num[2000]的使用 get char num[i] = new char[1000])
Time Limit: 10 second Memory Limit: 2 MB 问题描述 新年到了,突然间,就在那美丽的一霎那,你好友和你(K个人)的周围满是礼物,你发扬你帅气的风格,让你的好友先拿 ...
- View的绘制顺序
1.写在 super.onDraw() 的下面 把绘制代码写在 super.onDraw() 的下面,由于绘制代码会在原有内容绘制结束之后才执行,所以绘制内容就会盖住控件原来的内容. 2.写在 sup ...