【问题标题】:WPF: Show gif with ImageMagick by MemoryStreamWPF:通过 MemoryStream 使用 ImageMagick 显示 gif
【发布时间】:2018-10-10 20:58:50
【问题描述】:

我正在尝试通过普通的 Image 控件显示动画 gif:

<Image Name="animationImagePlayer"/>

集合包含之前加载的所有帧。

using (MemoryStream gifStream = new MemoryStream())
                {
                    collection.Write(gifStream, MagickFormat.Gif);
                    var bitmap = new BitmapImage();

                    bitmap.BeginInit();
                    bitmap.StreamSource = gifStream;
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    bitmap.Freeze();

                    animationImagePlayer.Source = bitmap;
                }

我看到序列的单帧并且没有动画机制。 我尝试将流保存在一个 gif 文件中,它可以完美运行。

【问题讨论】:

  • “单帧序列,没有动画机制”正是 Image 元素应该做的。你在网上搜索过吗? “wpf 动画 gif”?
  • 相同的解决方案适用于使用图片框的 winform,我不明白为什么它不应该在带有 Image 的 WPF 上使用
  • WPF 和 WinForms 是不同的框架。如果 WinForms PictureBox 显示动画 GIF,您不能断定 WPF Image 元素也可以。它只是没有。
  • 我愿意考虑一个可能的答案,一个关于要使用的控件类型的建议,或者使用 ImageMagick + MemoryStream 对解决方案进行通用的重新阐述。

标签: wpf imagemagick gif memorystream


【解决方案1】:

PictureBox 示例

  1. 你需要保持开放的流。

     System.IO.MemoryStream gifStream = new System.IO.MemoryStream();
    
  2. 在方法 Write() 中使用 MagickFormat.Gif 否则你在流中写入静态图像

     pictureEditor.image.Write(gifStream, MagickFormat.Gif);
     pictureBox1.Image = Image.FromStream(gifStream);
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2013-01-07
    • 2013-01-19
    • 2017-03-21
    • 1970-01-01
    • 2014-05-25
    相关资源
    最近更新 更多