在WPF中保存控件内容为图片

在WPF中保存控件内容为图片

实现代码如下

复制代码
 1   private void SaveControlContentAsImage(FrameworkElement element,string fileName)
 2         {
 3             var render = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Default);
 4             render.Render(element);
 5             BitmapEncoder encoder = new JpegBitmapEncoder();
 6             //BmpBitmapEncoder
 7             //GifBitmapEncoder
 8             //PngBitmapEncoder
 9             //TiffBitmapEncoder
10 
11             encoder.Frames.Add(BitmapFrame.Create(render));
12             using (System.IO.FileStream fs = System.IO.File.Create(fileName))
13             {
14                 encoder.Save(fs);
15             }
16                 
17         }

这里主要用到RenderTargetBitmapBitmapEncoder两个类。这里具体介绍一下这两个类。

RenderTargetBitmap

这个类的作用就是将 Visual 对象转换为位图。

使用步骤

1、通过构造函数构造一个RenderTargetBitmap对象

构造函数声明如下:

复制代码
1 public RenderTargetBitmap (int pixelWidth, int pixelHeight, double dpiX, double dpiY, System.Windows.Media.PixelFormat pixelFormat);

pixelWidth:位置的宽度

pixelHeight:位置的高度

dpiX:水平DPI

dpiY:垂直DPI

pixelFormat :位图的格式(使用PixelFormat枚举类型)

使用如下:

复制代码
 RenderTargetBitmap rtBitmap = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);        

2、调用Render(Visual)函数来渲染Visual对象

只要是Visual对象,都可以进行渲染。

在WPF介绍这篇文章中(https://www.cnblogs.com/zhaotianff/p/13373111.html),已经介绍了控件、面板、形状都是继承自Visual类,所以都可以用于Render函数渲染。

说明:

RenderTargetBitmap 类继承自System. Windows. Media. Imaging. BitmapSource ,所以可以直接用于设置Image 控件的ImageSource属性,并进行显示。

BitmapEncoder

这个类的作用是将 BitmapFrame 对象的集合编码为图像流。

使用方法如下

1、BitmapEncoder继承自以下类,根据想保存的图像格式,实例化一个BitmapEncoder对象

System.Windows.Media.Imaging.BmpBitmapEncoder

System.Windows.Media.Imaging.GifBitmapEncoder

System.Windows.Media.Imaging.JpegBitmapEncoder

System.Windows.Media.Imaging.PngBitmapEncoder

System.Windows.Media.Imaging.TiffBitmapEncoder

System.Windows.Media.Imaging.WmpBitmapEncoder

如想保存成jpg格式:

复制代码
1 BitmapEncoder encoder = new JpegBitmapEncoder();

2、设置图像内容

通过设置BitmapEncoder.Frame属性(System.Windows.Media.Imaging.BitmapFrame),可以设置图像内各帧。

BitmapFrame 类是表示由解码器返回并被编码器接受的图像数据。通俗点来说,就是BitampFrame代表图像编码过的帧。像JPG、BMP这样的静态图像,整个图像只有一帧。

BitmapFrame 拥有一个重载的Create 方法来创建BitmapFrame 对象。Create常用的三种形式如下:

* 通过流来创建BitmapFrame BitmapFrame.Create(System.IO.Stream)

* 通过Uri来创建BitmapFrame BitmapFrame.Create(Uri)

* 通过BitmapSource 来创建BitmapFrame BitmapFrame.Create(BitmapSource)

因为RenderTargetBitmap 是继承自BitmapSource类的,所以我们这里选择的是第三种重载。

复制代码
1 encoder.Frames.Add(BitmapFrame.Create(render));

3、保存图像

调用**BitmapEncoder.Save(System.IO.Stream)**函数来保存图像。因为这里只支持保存到流,不支持直接保存到文件,所以需要先创建文件,再进行保存。

推荐阅读

BitmapFrame Class (System.Windows.Media.Imaging) | Microsoft Learn

相关推荐
心平气和量大福大17 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
FuckPatience1 天前
Telerik UI for WPF 值不能为null。参数名:key
ui·wpf
lindexi1 天前
WPF 笔迹延迟优化从硬件到软件的全链路分析
wpf
weixin_727535622 天前
双Token认证体系深度拆解:Spring Security + JWT + Redis
redis·spring·wpf
liuxiaowei32 天前
Winform+WPF双框架实战:喷涂工艺SCADA上位机从0到1搭建(附采集监控源码+车间踩坑实录)
大数据·hadoop·wpf
贺国亚3 天前
模型训练-分布式与GPU调度
分布式·wpf
不羁的木木4 天前
HarmonyOS技术精讲-Connectivity Kit:实战——多屏协同与文件快传应用
华为·wpf·harmonyos
某不知名網友4 天前
C++ 从零实现负载均衡式在线 OJ 判题系统|分布式沙箱判题实战
wpf
fogota4 天前
【AI】C# .NET8 WPF 第三方DLL引用配置(编译不丢失、不自动删除)
c#·.net·wpf·dll
不羁的木木4 天前
HarmonyOS技术精讲-Connectivity Kit(短距通信服务):初识统一短距通信框架
华为·wpf·harmonyos