1

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

  1. 加载 Gif

    var myGif = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

  2. 获取框架

    var frame = myGif.Frames[i];

  3. 来自MSDN:Native Image Format Metadata Queries read (ushort)Metadata.GetQuery("/grctlext/Delay"), (ushort)Metadata.GetQuery("/imgdesc/Left"),(ushort)Metadata.GetQuery("/imgdesc/Top")

但是有两件事不起作用。首先,即使我尝试不同的动画 gif 文件,gif 和帧的元数据属性始终为空。其次,框架的 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");

        }
    }
}
4

1 回答 1

3

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

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

其次,frame.Metadata 返回一个没有GetQuery 方法的 ImageMetadata,但实际上返回的对象是具有 GetQuery 方法的BitmapMetadata类型,因此您只需像上次一样将 frame.Metadata 转换为 BitmapMetadata代码的注释行。

于 2011-01-03T22:42:33.343 回答