在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         }

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

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

相关推荐
samble3 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble11 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴13 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67514 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教14 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨16 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟16 天前
WPF(数据绑定)
wpf
samble17 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟18 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911121 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理