正在读取 GifBitmapDecoder 的 Metadata 属性...为什么它为空?
如何读取 gif 每一帧的延迟、左侧和顶部偏移数据?我已经走到这一步了。
-
加载 Gif
var myGif = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
-
获取一帧
var frame = myGif.Frames[i];
-
来自 MSDN:本机图像格式元数据查询读取
(ushort)Metadata.GetQuery("/grctlext/Delay")
、(ushort)Metadata.GetQuery("/imgdesc/Left")
、(ushort )Metadata.GetQuery("/imgdesc/Top")
但有两件事不起作用。首先,即使我尝试不同的动画 gif 文件,gif 和框架的元数据属性也始终为 null。其次,框架的 Metadata 属性似乎没有 GetQuery 方法。
我如何运行这些查询,我错过了什么?
编辑:
这是给我空元数据的示例代码。在全新的 WPF 应用程序上使用全新安装的 VS2010 Premium。图片文件就是评论里的那个。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var uri = new Uri(@"c:\b-414328-animated_gif_.gif");
var myGif = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
var frame = myGif.Frames[0];
Title = "";
Title += "Global Metadata is null: " + (myGif.Metadata == null).ToString();
Title += "; Frame Metadata is null: " + (frame.Metadata == null).ToString();
// Crash due to null metadata
//var frameData = (BitmapMetadata)frame.Metadata;
//var rate = (ushort)frameData.GetQuery("/grctlext/Delay");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要冻结要从中获取元数据的框架:
其次,frame.Metadata返回一个 ImageMetadata 它没有 GetQuery 方法,但实际上返回的对象的类型为 BitmapMetadata 具有 GetQuery 方法,因此您只需将 frame.Metadata 转换为 BitmapMetadata,就像在代码的最后注释行中所做的那样。
First, you need to Freeze the Frame you want to obtain the metadata from:
Second, the frame.Metadata returns an ImageMetadata which does not have a GetQuery method, but in fact the object returned is of type BitmapMetadata which has a GetQuery method, so you just need to cast frame.Metadata to BitmapMetadata as you do in the last commented lines of your code.