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 = 0, int height = 0, int x = 0, int y = 0){if (width == 0) width = (int)element.ActualWidth;if (height == 0) 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="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120" /><Label Content="姓名" Height="28" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="120" /><Label Content="年龄" Height="28" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="120" /><Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="120" /><Label Content="性别" Height="28" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="120" /><Button Command="{Binding ShowCommand}" Content="显示" Height="23" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="75" /><Button Content="控件截图" Name="BitMapBtn" Click="BitMapBtn_Click"Width="60" Height="30" Margin="0,30"></Button></Grid>
运行程序 ==》进入项目的debug目录下
转载于:https://www.cnblogs.com/likui-bookHouse/p/11114924.html
wpf 对控件进行截图,获取快照相关推荐
- 【转】WPF默认控件模板的获取和资源词典的使用
一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者如何在原有控件模板上修改的,下面就分享了获取某控件默认控件模板的方法(以控件Button ...
- C# WPF开源控件库:MahApps.Metro
❝ 其实站长很久之前就知道这个开源WPF控件库了,只是一直欣赏不了这种风格,但也star了该项目.每次浏览该仓库时,发现star越来越多,也看到很多网友对它的褒奖,所以今天就向大家推荐这款WPF控件库 ...
- wpf项目源代码_C# WPF开源控件库:MahApps.Metro
C# WPF开源控件库:MahApps.Metro ❝ 其实站长很久之前就知道这个开源WPF控件库了,只是一直欣赏不了这种风格,但也star了该项目.每次浏览该仓库时,发现star越来越多,也看到很多 ...
- 正则表达式——WPF输入控件TextBox 限定输入特定字符
正则表达式--WPF输入控件TextBox 限定输入特定字符 原文:正则表达式--WPF输入控件TextBox 限定输入特定字符 概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一 ...
- WPF 表格控件 ReoGrid 的简单使用
WPF 表格控件 ReoGrid 的简单使用 目录 一.概述 二.安装 三.添加控件 四.加载 Excel 五.属性设置 六.支持触摸滚动 七.其它操作 1.显示和隐藏列 2.显示特定字体 八.资源链 ...
- WPF开源控件扩展库 - MaterialDesignExtensions
WPF开源控件扩展库 - MaterialDesignExtensions MaterialDesignExtensions仓库截图 logo Material Design Extensions 在 ...
- Layui版本的WPF开源控件库-Layui-WPF
大家好,我是沙漠尽头的狼. 今天介绍一款Layui风格的WPF开源控件库,仓库信息如下: 仓库地址:https://github.com/Layui-WPF-Team/Layui-WPF 仓库截图: ...
- WPF(一) WPF基本控件与布局
WPF(Windows Presentation Foundation)是微软推出的基于Windows的用户界面框架,中文译为"Windows呈现基础",属于.NET Fram ...
- C# WPF 歌词控件(支持逐字定位描色效果)
C# WPF 歌词控件(支持逐字定位描色效果) 原文:C# WPF 歌词控件(支持逐字定位描色效果) 之前做了一个模仿网易云歌词的控件,实现了加载网易云歌词并能随音乐播放进度定位歌词.今天呢将在这个控 ...
最新文章
- PHP学习课程和培训方向学习路线分享
- Python 爬虫从入门到进阶之路(七)
- SpringMvc之整合DWR3.0.0推送消息
- 【学习笔记】Sass入门指南
- [leetcode]15.三数之和
- 设计模式(三)创建型模式
- [word技巧]把标题、图表题注编号由“一.1”改为“1.1“
- Lukas-Kanade光流法
- 常用类 (四) ----- Random随机数类
- 2 万字全面测评深度学习框架 PaddlePaddle、TensorFlow 和 Keras | 程序员硬核评测
- react for循环_5个很棒的 React.js 库,值得你亲手试试!
- 深度学习2.0-21.Keras高层API-compilefitEvaluatePredict
- mapreduce实现矩阵相乘
- java鼠标监听事件_Java鼠标事件监听器的创建和使用教程及代码
- 运动控制器PSO位置同步输出(一):硬件平台与PSO指令简介
- [Elasticsearch] 多字段搜索 (五) - 以字段为中心的查询
- 百度EasyDL实体抽取和关系抽取
- Ajax介绍和基本使用
- 如何修复手机里的m4a格式录音文件
- java kafka consumer不消费,报错marking the coordinator (id rack null) dead for group
热门文章
- c#中分割字符串的几种方法
- uscao Spinning Wheels
- (一次性搞定)ORB_SLAM2地图保存与加载
- teamviewer设备数量上限_智能控制设备连接故障排除
- oracle网站注入,oracle注入,utl_http方法
- git ssh创建分支_Git(2):在gitlab中创建开发用户,以及master分支的安全管理
- 如何重装计算机操作系统,自己如何重装笔记本电脑操作系统呢?
- html5下拉智能,HTML5新增标签 + 智能表单
- idea怎么找到当前报错的行_科研论文如何想到不错的 idea?
- PTA ---结构错题汇总