有时候我们项目,在执行某个操作后,会生成一些数据结果,如报表一类的东西,我们需要对结果进行保存,甚至是生成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 对控件进行截图,获取快照的更多相关文章

  1. WPF默认控件模板的获取和资源词典的使用

    一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1.创建 ...

  2. WPF 对控件进行截图且不丢失范围(转载)

    原文:Taking WPF “Screenshots” I was recently working on a Surface project at Microsoft (that will be s ...

  3. WPF常用控件应用demo

    WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...

  4. C# WPF开源控件库:MahApps.Metro

    其实站长很久之前就知道这个开源WPF控件库了,只是一直欣赏不了这种风格,但也star了该项目.每次浏览该仓库时,发现star越来越多,也看到很多网友对它的褒奖,所以今天就向大家推荐这款WPF控件库. ...

  5. WPF Image控件中的ImageSource与Bitmap的互相转换

    原文:WPF Image控件中的ImageSource与Bitmap的互相转换  1.从bitmap转换成ImageSource [DllImport("gdi32.dll", ...

  6. 正则表达式——WPF输入控件TextBox 限定输入特定字符

    概念: 正则表达式是对字符串操作的一种逻辑公式, 就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”, 这个“规则字符串”用来表达对字符串的一种过滤逻辑. 目的: 给定一个正 ...

  7. WPF DataGrid 控件的运用

    WPF DataGrid 控件的运用 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-23 参考: King Cobra 博客 ...

  8. WPF布局控件常用属性介绍

    WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0)   概述:WPF布局控件都是派生自System.Windows ...

  9. C# WPF 歌词控件(支持逐字定位描色效果)

    原文:C# WPF 歌词控件(支持逐字定位描色效果) 之前做了一个模仿网易云歌词的控件,实现了加载网易云歌词并能随音乐播放进度定位歌词.今天呢将在这个控件的基础上增加逐字定位描色功能,如下图效果(QQ ...

随机推荐

  1. 【18NOIP普及组】对称二叉树(信息学奥赛一本通 1981)(洛谷 5018)

    [题目描述] 一棵有点权的有根树如果满足以下条件,则被轩轩称为对称二叉树: 1.二叉树: 2.将这棵树所有节点的左右子树交换,新树和原树对应位置的结构相同且点权相等. 下图中节点内的数字为权值,节点外 ...

  2. 两个对象值相同 (x.equals(y) == true) ,但却可有不同的 hash code ,这 句话对不对

    答:不对,有相同的 hash code这是java语言的定义:1) 对象相等则hashCode一定相等:2) hashCode相等对象未必相等 1.如果是基本变量,没有hashcode和equals方 ...

  3. 第09组 Beta冲刺(1/4)

    队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...

  4. 008 webpack的其他使用方式

    一:配置 1.配置文件 每次修改main文件,重新打包都要指定入口与出口,比较费事,可以使用配置文件的方式 在根目录下新建webpack.config.js: const path = require ...

  5. c++ 字符串时间格式转换为时间 判断有效期

    转载:https://www.cnblogs.com/maphc/p/3462952.html #include <iostream> #include <time.h> us ...

  6. 使用expect快速登录线上机器

    背景: 公司登陆线上服务器一般都要经过跳板机才能登陆,过程比较麻烦,如果要频繁登陆某个机器而且机器名不好记很费劲,使用expect自动登陆会方便很多 实现: 使用expect脚本自动登陆: #!/us ...

  7. VC++6.0/VC6使用c99的stdint.h

    如果使用https://github.com/mattn/gntp-send/blob/master/include/msinttypes/stdint.h会报错: error C2733: seco ...

  8. git让线上代码强制覆盖本地的

    git强制覆盖本地命令(分步执行): git fetch --all    git reset --hard origin/master    git pull git强制覆盖本地命令(单条执行):  ...

  9. 【原创】C++11:左值和右值(深度分析)

    ——原创,引用请附带博客地址 2019-12-06 23:42:18 这篇文章分析的还是不行,先暂时放在这以后再更新. 本篇比较长,需要耐心阅读 以一个实际问题开始分析 class Sub{} Sub ...

  10. 【VS开发】#pragma pack(push,1)与#pragma pack(1)的区别

    这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n)             作用:C编译器将按照n个字节对 ...