如何将 BitmapImage 从内存中保存到 WPF C# 中的文件中? [英] How do I save a BitmapImage from memory into a file in WPF C#?

查看:31
本文介绍了如何将 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# 中的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆