wpf 对控件进行截图,获取快照
有时候我们项目,在执行某个操作后,会生成一些数据结果,如报表一类的东西,我们需要对结果进行保存,甚至是生成word文档。
那么首先获取到控件快照就最基本的条件。
生成快照的静态方法类
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging; namespace MvvmFuncationApp.Model
{
public static class ToBitmapTool
{
/// <summary>
/// 截图转换成bitmap
/// </summary>
/// <param name="element"></param>
/// <param name="width">默认控件宽度</param>
/// <param name="height">默认控件高度</param>
/// <param name="x">默认0</param>
/// <param name="y">默认0</param>
/// <returns></returns>
public static Bitmap ToBitmap(this FrameworkElement element, int width = , int height = , int x = , int y = )
{
if (width == ) width = (int)element.ActualWidth;
if (height == ) height = (int)element.ActualHeight; var rtb = new RenderTargetBitmap(width, height, x, y, System.Windows.Media.PixelFormats.Default);
rtb.Render(element);
var bit = BitmapSourceToBitmap(rtb); //测试代码
DirectoryInfo d = new DirectoryInfo(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Cache"));
if (!d.Exists) d.Create();
bit.Save(System.IO.Path.Combine(d.FullName, "控件截图.png")); return bit;
} /// <summary>
/// BitmapSource转Bitmap
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap BitmapSourceToBitmap(this BitmapSource source)
{
return BitmapSourceToBitmap(source, source.PixelWidth, source.PixelHeight);
} /// <summary>
/// Convert BitmapSource to Bitmap
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap BitmapSourceToBitmap(this BitmapSource source, int width, int height)
{
Bitmap bmp = null;
try
{
PixelFormat format = PixelFormat.Format24bppRgb;
/*set the translate type according to the in param(source)*/
switch (source.Format.ToString())
{
case "Rgb24":
case "Bgr24": format = PixelFormat.Format24bppRgb; break;
case "Bgra32": format = PixelFormat.Format32bppPArgb; break;
case "Bgr32": format = PixelFormat.Format32bppRgb; break;
case "Pbgra32": format = PixelFormat.Format32bppArgb; break;
}
bmp = new Bitmap(width, height, format);
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
ImageLockMode.WriteOnly,
format);
source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
bmp.UnlockBits(data);
}
catch
{
if (bmp != null)
{
bmp.Dispose();
bmp = null;
}
} return bmp;
}
}
}
添加按钮事件:
private void BitMapBtn_Click(object sender, RoutedEventArgs e)
{
// StudentInfoGrid =======>控件的名称
ToBitmapTool.ToBitmap(StudentInfoGrid); }
}
前台代码:
<Grid Background="Gray" Name="StudentInfoGrid">
<Label Content="学号" Height="" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="" />
<Label Content="姓名" Height="" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" Height="" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="" />
<Label Content="年龄" Height="" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" Height="" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="" />
<Label Content="Email" Height="" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" Height="" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="" />
<Label Content="性别" Height="" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" />
<TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" Height="" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="" />
<Button Command="{Binding ShowCommand}" Content="显示" Height="" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="" />
<Button Content="控件截图" Name="BitMapBtn" Click="BitMapBtn_Click"
Width="" Height="" Margin="0,30"></Button>
</Grid>
运行程序 ==》进入项目的debug目录下


wpf 对控件进行截图,获取快照的更多相关文章
- WPF默认控件模板的获取和资源词典的使用
一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1.创建 ...
- WPF 对控件进行截图且不丢失范围(转载)
原文:Taking WPF “Screenshots” I was recently working on a Surface project at Microsoft (that will be s ...
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- C# WPF开源控件库:MahApps.Metro
其实站长很久之前就知道这个开源WPF控件库了,只是一直欣赏不了这种风格,但也star了该项目.每次浏览该仓库时,发现star越来越多,也看到很多网友对它的褒奖,所以今天就向大家推荐这款WPF控件库. ...
- WPF Image控件中的ImageSource与Bitmap的互相转换
原文:WPF Image控件中的ImageSource与Bitmap的互相转换 1.从bitmap转换成ImageSource [DllImport("gdi32.dll", ...
- 正则表达式——WPF输入控件TextBox 限定输入特定字符
概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...
- WPF DataGrid 控件的运用
WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...
- WPF布局控件常用属性介绍
WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0) 概述:WPF布局控件都是派生自System.Windows ...
- C# WPF 歌词控件(支持逐字定位描色效果)
原文:C# WPF 歌词控件(支持逐字定位描色效果) 之前做了一个模仿网易云歌词的控件,实现了加载网易云歌词并能随音乐播放进度定位歌词.今天呢将在这个控件的基础上增加逐字定位描色功能,如下图效果(QQ ...
随机推荐
- 「ZJOI2019」Minmax搜索
传送门 Solution 叶子节点的变化区间是连续的,可得知非叶子节点的权值变化区间也是连续的 由此可知,\(W\)的变化值的可行域也是连续的,所以只需要看它能否变为\(W+1\)或\(W-1\) 对 ...
- Centos 7配置nginx反向代理负载均衡集群
一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76 nginx负载均衡器 192.168.2.82 web ...
- Prometheus监控神技--自动发现配置
一.自动发现类型 在上一篇文中留了一个坑: 监控某个statefulset服务的时候,我在service文件中定义了个EP,然后把pod的ip写死在配置文件中,这样,当pod重启后,IP地址变化,就监 ...
- deepin深度学习环境配置
deepin是一个精致优美的系统.最近因为工作需要在deepin上配置深度学习环境,话不多说,接下来记录下整个的配置过程. ×××本篇文章适合对深度学习环境配置有一定了解且对deepin系统感兴趣的同 ...
- Spark安装(standalone)
文档:http://spark.apache.org/docs/latest/spark-standalone.html 安装scalahttps://www.scala-lang.org/downl ...
- PHP正则匹配中文汉字会得到�
preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}]+$/u', $str)
- [转]Maven 全局配置文件settings.xml详解
原文地址:https://www.jianshu.com/p/110d897a5442 概要 settings.xml有什么用? 如果在Eclipse中使用过Maven插件,想必会有这个经验:配置se ...
- EF6中的SQL监控
在MVC或WEBAPI中的监控 System.Action<string> action = (string message) => { Debug.WriteLine(messag ...
- Hashmap(类似字典的东西)
注意: 键值是唯一的,1个键对应一个值 常用api 打印处字典直接println方法 判断是否存在key值 containsKey() 例子: 基础操作 https://ke.qq.com/w ...
- Tensorflow问题
TypeError: 'urban' has type str, but expected one of: bytes 在前面添加"b"(例如,b'urban'),或者处理为var ...