【发布时间】:2013-09-14 04:27:28
【问题描述】:
我正在尝试编写一些代码来使用 GifBitmapEncoder 从 WPF 应用程序中导出动画 .gif。到目前为止我的工作正常,但是当我查看生成的 .gif 时,它只运行一次然后停止 - 我想让它无限循环。
我发现了这个以前的类似问题:
How do I make a GIF repeat in loop when generating with BitmapEncoder
但是,他使用的是 Windows.Graphics.Imaging 中的 BitmapEncoder,而不是 Windows.Media.Imaging 版本,这似乎有点不同。尽管如此,这给了我一个方向,经过更多的谷歌搜索后,我想出了这个:
Dim encoder As New GifBitmapEncoder
Dim metaData As New BitmapMetadata("gif")
metaData.SetQuery("/appext/Application", System.Text.Encoding.ASCII.GetBytes("NETSCAPE2.0"))
metaData.SetQuery("/appext/Data", New Byte() {3, 1, 0, 0, 0})
'The following line throws the exception "The designated BitmapEncoder does not support global metadata.":
'encoder.Metadata = metaData
If DrawingManager.Instance.SelectedFacing IsNot Nothing Then
For Each Frame As Frame In DrawingManager.Instance.SelectedFacing.Frames
Dim bmpFrame As BitmapFrame = BitmapFrame.Create(Frame.CombinedImage, Nothing, metaData, Nothing)
encoder.Frames.Add(bmpFrame)
Next
End If
Dim fs As New FileStream(newFileName, FileMode.Create)
encoder.Save(fs)
fs.Close()
最初我尝试将元数据直接添加到编码器(如上面代码中注释掉的行),但在运行时抛出异常“指定的 BitmapEncoder 不支持全局元数据”。相反,我可以将我的元数据附加到每个帧,但是虽然这不会导致它崩溃,但生成的 .gif 也不会循环(我希望循环的元数据无论如何都需要是全局的)。
谁能给点建议?
【问题讨论】:
-
为什么不使用 Windows.Graphics.Imaging?
-
对不起,我应该说 - Windows.Graphics.Imaging 似乎只能在 Windows 8 环境中使用,这对我没有好处。即使不是这样,我也希望避免在项目中添加任何我不需要的依赖项。
-
根据 MSDN:“图形交换格式 (GIF) 图像不支持全局预览、全局缩略图、全局元数据、帧级缩略图或帧级元数据。”
-
您找到合适的解决方案了吗?
-
@Prof:恐怕不是,因为它是一个业余项目的非必要部分,我让它滑动。我的两个最佳选择似乎要么放弃 GifBitmapEncoder 并推出我自己的 .gif 编写器,要么使用它写出 gif,然后将适当的应用程序扩展名插入文件中。我仍然有兴趣找到一种简单的方法,所以如果你找到了一些东西,请告诉我!
标签: c# wpf vb.net animated-gif