如何将 BitmapImage 从内存中保存到 WPF C# 中的文件中?

How do I save a BitmapImage from memory into a file in WPF C#?(如何将 BitmapImage 从内存中保存到 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# 中的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)