本文介绍了如何将 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!

推荐答案

您需要使用编码器来保存图像.PngBitmapEncoder 将处理大多数常见的图像类型(PNG、BMP、TIFF 等).以下将拍摄图像并保存:

You need to use an encoder to save the image. The PngBitmapEncoder will handle most of the common image types (PNG, BMP, TIFF, etc). 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# 中的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:25