在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

相关推荐
The Sheep 202314 小时前
WPF自定义路由事件
大数据·hadoop·wpf
阳光雨滴14 小时前
使用wpf用户控件编程落石效果动画
c++·wpf
wuty00715 小时前
WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离
wpf·sendmessage·changewindowmessagefilterex·uip·消息筛选器的用户界面特权隔离·window message
攻城狮CSU21 小时前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机21 小时前
wpf之Interaction.Triggers
c#·wpf
是木子啦1 天前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 20231 天前
wpf 命令理解
wpf
布伦鸽1 天前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
分布式存储与RustFS2 天前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU2 天前
WPF 绑定机制实现原理
wpf