在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

相关推荐
self_myth12 小时前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
c#上位机13 小时前
wpf之TextBlock
c#·wpf
玉面小君2 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia
c#上位机2 天前
wpf之Border
c#·wpf
SunflowerCoder2 天前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
周杰伦fans2 天前
WPF中的DataContext以及常见的绑定方式
wpf
没有bug.的程序员3 天前
Redis 数据结构全面解析:从底层编码到实战应用
java·数据结构·redis·wpf
somethingGoWay3 天前
wpf 自定义输入ip地址的文本框
wpf
秋月的私语3 天前
Wpf程序屏幕居中问题修复全记录
wpf
我要打打代码3 天前
WPF启动窗体的三种方式
wpf