在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

相关推荐
芝麻科技3 小时前
Wpf使用NLog将日志输出到LogViewer
wpf·prism
△曉風殘月〆21 小时前
WPF颜色(SolidColorBrush)和Win32颜色(COLOREF)互转的方法
wpf·win32·solidcolorbrush·colorref
shanshan20991 天前
上位机系统架构 | 如何设计一个高效的多相机管理系统
c#·wpf·相机
充值内卷1 天前
WPF入门教学四 WPF控件概述
windows·ui·wpf
小白2 天前
WPF自定义Dialog模板,内容用不同的Page填充
wpf
Crazy Struggle2 天前
C# + WPF 音频播放器 界面优雅,体验良好
c#·wpf·音频播放器·本地播放器
James.TCG3 天前
WPF动画
wpf
He BianGu3 天前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf
脚步的影子4 天前
.NET 6.0 + WPF 使用 Prism 框架实现导航
.net·wpf