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

一、目的:将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 格式
cs 复制代码
        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 格式
cs 复制代码
        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 格式
cs 复制代码
        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个人主页-哔哩哔哩视频

相关推荐
智者知已应修善业34 分钟前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
sakiko_1 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Alice-YUE2 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
小陈phd3 小时前
TensorRT 入门完全指南(一)——从核心定义到生态工具全解析
人工智能·笔记
是上好佳佳佳呀3 小时前
【前端(十一)】JavaScript 语法基础笔记(多语言对比)
前端·javascript·笔记
handler013 小时前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法
其实防守也摸鱼4 小时前
CTF密码学综合教学指南--第四章
网络·笔记·安全·网络安全·密码学·ctf
05候补工程师6 小时前
【ROS 2 具身智能】Gazebo 仿真避坑指南:从“幽灵机器人”到传感器数据流打通
人工智能·经验分享·笔记·ubuntu·机器人
chushiyunen6 小时前
pandas使用笔记、数据清洗、json_normalize
笔记·pandas
HERR_QQ6 小时前
端到端课程自用 4 规划 基于自规划AR的端到端规划 AI 笔记
人工智能·笔记·自动驾驶·transformer