笔记:将WPF中可视化元素(Visual)保存为图像,如PNG,JPEG或BMP的方法简介

devtools/2024/9/25 8:59:02/

一、目的:将WPF中可视化元素(Visual)保存为图像,如PNG,JPEG或BMP的方法简介

        BitmapEncoder 是 WPF 中用于将图像数据编码为特定格式的基类。它提供了将 BitmapSource 对象保存为各种图像格式(如 PNG、JPEG、BMP 等)的功能。PngBitmapEncoder 是 BitmapEncoder 的一个具体实现,用于将图像数据编码为 PNG 格式。

        PngBitmapEncoder 是 WPF 中用于将图像数据编码为 PNG 格式的类。它属于 System.Windows.Media.Imaging 命名空间。使用 PngBitmapEncoder 可以将 BitmapSource 对象保存为 PNG 文件。

        要将图像保存为其他格式,如 JPEG 或 BMP,你可以使用 JpegBitmapEncoderBmpBitmapEncoder 类。这些类与 PngBitmapEncoder 类似,都是 BitmapEncoder 的具体实现。以下是如何将图像保存为 JPEG 和 BMP 格式的示例。


二、实现

步骤:

1、将 Visual 渲染到 RenderTargetBitmap  中

2、应用BmpBitmapEncoder 保存为对应的文件

示例代码

            int width = 200;int height = 200;// 使用 DrawingVisual 和 DrawingContext 绘制图形DrawingVisual drawingVisual = new DrawingVisual();using (DrawingContext drawingContext = drawingVisual.RenderOpen()){drawingContext.DrawRectangle(Brushes.Blue, null, new Rect(50, 50, 100, 100));drawingContext.DrawText(new FormattedText("Hello, WPF!",System.Globalization.CultureInfo.InvariantCulture,FlowDirection.LeftToRight,new Typeface("Verdana"),32,Brushes.White),new Point(60, 60));drawingContext.DrawDrawing(new GeometryDrawing(Brushes.Red, new Pen(Brushes.Black, 1), new RectangleGeometry(new Rect(10, 10, 50, 50))));}// 将 DrawingVisual 渲染到 RenderTargetBitmap  中RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);renderTargetBitmap.Render(drawingVisual);// 保存为 PNG 文件SaveAsPng(renderTargetBitmap, "output.png");// 保存为 JPEG 文件SaveAsJpeg(renderTargetBitmap, "output.jpg");// 保存为 BMP 文件SaveAsBmp(renderTargetBitmap, "output.bmp");
将图像保存为 PNG 格式
        public void SaveAsPng(RenderTargetBitmap renderTargetBitmap, string filePath){PngBitmapEncoder encoder = new PngBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create)){encoder.Save(stream);}}
png文件结果 

 

将图像保存为 JPEG 格式
        public void SaveAsJpeg(RenderTargetBitmap renderTargetBitmap, string filePath){JpegBitmapEncoder encoder = new JpegBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create)){encoder.Save(stream);}}
jpg文件结果  

 

将图像保存为 BMP 格式 
public void SaveAsBmp(RenderTargetBitmap renderTargetBitmap, string filePath){BmpBitmapEncoder encoder = new BmpBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create)){encoder.Save(stream);}}
bmp文件结果  

 

 

需要了解的知识点 

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

PngBitmapEncoder 类 (System.Windows.Media.Imaging) | Microsoft Learn 

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

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

RenderTargetBitmap 类 (System.Windows.Media.Imaging) | Microsoft Learn 

DrawingVisual Class (System.Windows.Media) | Microsoft Learn 

System.Windows.Controls 命名空间 | Microsoft Learn

控件库 - WPF .NET Framework | Microsoft Learn

WPF 介绍 | Microsoft Learn

XAML概述 - WPF .NET | Microsoft Learn

Windows Presentation Foundation 简介 - WPF .NET | Microsoft Learn

使用 Visual Studio 创建新应用教程 - WPF .NET | Microsoft Learn

源码地址

GitHub - HeBianGu/WPF-ControlDemo: 示例

GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库

GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库

了解更多

适用于 .NET 8 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

适用于 .NET 7 的 WPF 的新增功能 - WPF .NET | Microsoft Learn

System.Windows.Controls 命名空间 | Microsoft Learn

Reference Source

Sysinternals - Sysinternals | Microsoft Learn

Windows app development documentation - Windows apps | Microsoft Learn

欢迎使用 Expression Blend | Microsoft Learn

https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/?view=netdesktop-7.0&WT.mc_id=MVP_380318

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频


http://www.ppmy.cn/devtools/116886.html

相关文章

Oracle 数据库部署与实施

文章目录 1. macOS 上部署 Oracle 数据库通过 Docker 在 macOS 上部署 2. Linux 上部署 Oracle 数据库直接在 Linux 上部署通过 Docker 在 Linux 上部署 3. Windows 上部署 Oracle 数据库4. 使用 Docker 部署 Oracle 数据库前提条件拉取 Oracle 数据库 Docker 镜像运行 Oracle …

计算机知识科普问答--16(76-80)

文章目录 76、什么是处理机调度?调度算法主要有哪几种?1. **处理机调度(Processor Scheduling)**2. **处理机调度的分类**3. **常见的调度算法**(1)**先来先服务(First-Come, First-Served, FCFS)**(2)**短作业优先(Shortest Job First, SJF)**(3)**优先级调度(…

【Mysql】Centos 安装 Mysql8.0

官网下载安装包 官网地址:MySQL :: Download MySQL Community Server 查看服务器的名称和版本号 lsb_release -a 查看服务的架构 uname -m 下载对应的版本,这里操作系统选择 Red Hat 就可以了。(CentOS 就是将 RHEL 发行的源代码从新编译…

爬虫的流程

爬虫的流程 获取网页提取信息保存数据自动化程序能爬怎样的数据 获取网页 获取网页就是获取网页的源代码,源代码里包含了网页的部分有用信息,所以只要把源代码获取下来,就可以从中提取想要的信息浏览器访问网页的本质:浏览器向服…

实验室ICPR 2024论文分享┆FPMT: 基于增强型半监督模型的交通事件检测(含详细视频解读)

目录 论文分享简介 1. 会议介绍 2. 研究背景及主要贡献 3. 方法 4. 实验 5. 结论 6. 论文介绍视频 论文分享简介 本推文详细介绍了一篇实验室的最新论文成果《FPMT: Enhanced Semi-Supervised Model for Traffic Incident Detection》,该论文已被第27届国际…

程序设计中,day 与 date 这2个单词的区别

先看下面几个例句感受一下: What day is it today? - Its Firday. Whats the date today? - Its September 10th, Teachers Day. It takes me 5 days to read the book. 在不同的编程语言或数据库中,"day" 和 "dat…

python网站创建001:内容概览

内容概览: 1. Python环境搭建(Python解释器、Pycharm、环境变量等) 2. 基础语法(条件、循环、输入输出、编码等) 3. 数据类型型(整型、布尔型、字符串、列表、字典、元组、集合等) 4. 函数&…

机器学习与深度学习

目录 机器学习深度学习机器学习和深度学习有哪些维度的不同? 机器学习 首先简要介绍下机器学习(Machine Learning)的基本概念。主要介绍机器学习算法的应用,监督学习和无监督学习(supervised-unsupervised learning&a…