读取 GifBitmapDecoder 的 Metadata 属性...为什么它为空? [英] Reading Metadata property of GifBitmapDecoder...why is it null?

查看:21
本文介绍了读取 GifBitmapDecoder 的 Metadata 属性...为什么它为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取 gif 的每一帧的延迟、左侧和顶部偏移数据?我已经走到这一步了.

How can I read the delay, left and top offset data for each frame of a gif? I've gotten this far.

  1. 加载 Gif

  1. Load the 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 文件.其次,frame 的 Metadata 属性似乎没有 GetQuery 方法.

But two things don't work. First the Metadata property of both the gif and the frame are always null, even if I try different animated gif files. Second, the Metadata property of the frame doesn't seem to have a GetQuery method.

我如何运行这些查询,我错过了什么?

How do I run these queries, what did I miss?

这是给我提供空元数据的示例代码.在全新的 WPF 应用程序上使用全新安装的 VS2010 Premium.图片文件是评论中的那个.

Here is sample code that gives me null metadata. Using a fresh install of VS2010 Premium, on a fresh WPF application. The image file is the one in the comments.

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");

        }
    }
}

推荐答案

首先,您需要 冻结要从中获取元数据的帧:

First, you need to Freeze the Frame you want to obtain the metadata from:

var frame = myGif.Frames[0];
frame.Freeze();

其次,frame.Metadata 返回一个 ImageMetadata 没有 GetQuery 方法,但实际上返回的对象类型为 BitmapMetadata,它有一个 GetQuery 方法,因此您只需要将 frame.Metadata 转换为 BitmapMetadata,就像您在代码的最后注释行中所做的那样.

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.

这篇关于读取 GifBitmapDecoder 的 Metadata 属性...为什么它为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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