简体   繁体   English

如何将BitmapFrame转换为BitmapImage

[英]How to convert BitmapFrame into BitmapImage

My program uses some WPF APIs to process images in a folder. 我的程序使用一些WPF API来处理文件夹中的图像。 I need to parse EXIF data from the images if there is any in order to get the value of the DateTimeOriginal property. 我需要从图像中解析EXIF数据(如果有)以便获取DateTimeOriginal属性的值。 I've been using a BitmapImage to load the image from a MemoryStream , but I need a BitmapFrame object to get at the BitmapMetadata object. 我一直在使用BitmapImageMemoryStream加载图像,但是我需要一个BitmapFrame对象来获取BitmapMetadata对象。

Here's my original code before I was given the requirement to get the DateTimeOriginal EXIF property: 这是在要求获得DateTimeOriginal EXIF属性之前的原始代码:

BitmapImage src = new BitmapImage();
try {
    using ( MemoryStream memoryStream = new MemoryStream( snapshot.ImageData ) ) {
        src.BeginInit();
        src.CacheOption  = BitmapCacheOption.OnLoad;
        src.StreamSource = memoryStream;
        src.EndInit();
    }
} catch ( NotSupportedException ex ) {
    . . .
}

Now, to get the BitmapFrame , I need to create a BitmapDecoder and use the Frames[0] property. 现在,要获取BitmapFrame ,我需要创建一个BitmapDecoder并使用Frames[0]属性。 So I added the following right after the using statement: 因此,我在using语句之后添加了以下内容:

BitmapDecoder decoder = BitmapDecoder.Create( memoryStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad );

This decodes the bitmap and gets the first Frame, which is cool. 这将解码位图并获取第一个Frame,这很酷。 I still need to create the BitmapImage . 我仍然需要创建BitmapImage I can do that by resetting the MemoryStream's position back to the beginning and keeping the code that's already there, but this will decode the same image a second time. 我可以通过将MemoryStream's位置重新设置为开始并保留已经存在的代码来做到这一点,但这将再次解码同一张图像。

Is there a way I can convert the BitmapFrame into a BitmapImage ? 有没有一种方法可以将BitmapFrame转换为BitmapImage I thought I would cast the BitmapFrame into a BitmapSource object and then set the BitmapImage's Source propert, but the BitmapImage class doesn't seem to have a Source property. 我以为我可以将BitmapFrame转换为BitmapSource对象,然后设置BitmapImage's Source Source属性,但是BitmapImage类似乎没有Source属性。

Is this possible? 这可能吗? If so, how is it done? 如果是这样,怎么做?

There should be no need for this conversion. 无需进行此转换。 The Remarks section in the BitmapImage MSDN page says: BitmapImage MSDN页面中的“备注”部分显示:

BitmapImage primarily exists to support Extensible Application Markup Language (XAML) syntax and introduces additional properties for bitmap loading that are not defined by BitmapSource. BitmapImage主要是为了支持可扩展应用程序标记语言(XAML)语法而存在的,并引入了BitmapSource未定义的其他位图加载属性。

When using bitmaps, your application should usually always use the BitmapSource or ImageSource base classes. 使用位图时,您的应用程序通常应始终使用BitmapSourceImageSource基类。 For example when you assign the Source property of the WPF Image control, you can use an instance of any type derived from ImageSource , because that is the type of the Source property. 例如,当您分配WPF Image控件的Source属性时,可以使用从ImageSource派生的任何类型的实例,因为这是Source属性的类型。

So you could directly use a BitmapFrame because it also derives from ImageSource : 因此,您可以直接使用BitmapFrame因为它也源自ImageSource

var bitmap = BitmapFrame.Create(
    memoryStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

image.Source = bitmap;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM