How do I save a BitmapImage from memory into a file in WPF C#?(如何将 BitmapImage 从内存中保存到 WPF C# 中的文件中?)
问题描述
我在这方面找不到任何东西,需要一些帮助.我已经将一堆图像作为 BitmapImage 类型加载到内存中,这样我就可以删除它们存储的临时目录.我已经成功完成了这部分.现在我需要将图像保存到不同的临时位置,但我不知道如何执行此操作图像包含在:
I can't find anything over this and need some help. I have loaded a bunch of images into memory as BitmapImage types, so that I can delete the temp directory that they were stored in. I have successfully done this part. Now I need to save the images to a different temp location and I can't figure out how to do this The images are contained in a:
Dictionary<string, BitmapImage>
字符串是文件名.如何将此集合保存到新的临时位置?感谢您的帮助!
The string is the filename. How do I save this collection to the new temp location? Thanks for any help!
推荐答案
您需要使用编码器来保存图像.以下将获取图像并保存:
You need to use an encoder to save the image. The following will take the image and save it:
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
encoder.Save(fileStream);
}
我通常会将其写入扩展方法,因为它是图像处理/操作应用程序的一个非常常见的功能,例如:
I usually will write this into an extension method since it's a pretty common function for image processing/manipulating applications, such as:
public static void Save(this BitmapImage image, string filePath)
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
encoder.Save(fileStream);
}
}
这样您就可以从 BitmapImage 对象的实例中调用它.
This way you can just call it from the instances of the BitmapImage objects.
这篇关于如何将 BitmapImage 从内存中保存到 WPF C# 中的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 BitmapImage 从内存中保存到 WPF C# 中的文件中?
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01