wpf 如何将 BitmapFrame 转换为 BitmapImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22467798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:14:50  来源:igfitidea点击:

How to convert BitmapFrame into BitmapImage

c#wpf

提问by Tony Vitabile

My program uses some WPF APIs to process images in a folder. I need to parse EXIF data from the images if there is any in order to get the value of the DateTimeOriginalproperty. I've been using a BitmapImageto load the image from a MemoryStream, but I need a BitmapFrameobject to get at the BitmapMetadataobject.

我的程序使用一些 WPF API 来处理文件夹中的图像。我需要从图像中解析 EXIF 数据(如果有的话)以获得DateTimeOriginal属性的值。我一直在使用 aBitmapImage从 a 加载图像MemoryStream,但我需要一个BitmapFrame对象来获取该BitmapMetadata对象。

Here's my original code before I was given the requirement to get the DateTimeOriginalEXIF property:

这是我被要求获得DateTimeOriginalEXIF 属性之前的原始代码:

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 BitmapDecoderand use the Frames[0]property. So I added the following right after the usingstatement:

现在,要获得BitmapFrame,我需要创建一个BitmapDecoder并使用该Frames[0]属性。所以我在using声明之后添加了以下内容:

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

This decodes the bitmap and gets the first Frame, which is cool. I still need to create the BitmapImage. I can do that by resetting the MemoryStream'sposition back to the beginning and keeping the code that's already there, but this will decode the same image a second time.

这对位图进行解码并获得第一帧,这很酷。我仍然需要创建BitmapImage. 我可以通过将MemoryStream's位置重置回开头并保留已经存在的代码来做到这一点,但这将再次解码相同的图像。

Is there a way I can convert the BitmapFrameinto a BitmapImage? I thought I would cast the BitmapFrameinto a BitmapSourceobject and then set the BitmapImage's Sourcepropert, but the BitmapImageclass doesn't seem to have a Sourceproperty.

有没有办法可以将 转换BitmapFrameBitmapImage?我以为我会将其BitmapFrame转换为BitmapSource对象,然后设置属性BitmapImage's Source,但BitmapImage该类似乎没有Source属性。

Is this possible? If so, how is it done?

这可能吗?如果是这样,它是如何完成的?

回答by Clemens

There should be no need for this conversion. The Remarks section in the BitmapImageMSDN page says:

应该不需要这种转换。BitmapImageMSDN 页面中的备注部分说:

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 BitmapSourceor ImageSourcebase classes. For example when you assign the Sourceproperty of the WPF Image control, you can use an instance of any type derived from ImageSource, because that is the type of the Sourceproperty.

使用位图时,您的应用程序通常应始终使用BitmapSourceImageSource基类。例如,当您分配SourceWPF Image 控件的属性时,您可以使用从 派生的任何类型的实例ImageSource,因为这是Source属性的类型。

So you could directly use a BitmapFramebecause it also derives from ImageSource:

所以你可以直接使用 aBitmapFrame因为它也来自ImageSource

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

image.Source = bitmap;