当前位置:首页>网络学院>程序开发>c#教程>文章内容

在WPF中显示动态GIF

[ 来源:http://www.it55.com | 作者: | 时间:2007-12-24 | 收藏 | 推荐 ] 【


   {
   //创建无动画的普通Image
   CreateNonGifAnimationImage();
   }
   }
   }
   else
   {
   //创建无动画的普通Image
   CreateNonGifAnimationImage();
   }
   }
  当读取文件成功后,一切都好办了,通过解析内存流中的数据,我们可以得到足够多的信息,比如帧的列表,每帧显示的时间以及该帧显示完成后如何处理,那么我们就可以用一个计时器(DispatcherTimer)来处理这一切而形成一个动画了.
   /**//// <summary>
   /// 从内存流中创建图片
   /// </summary>
   public void CreateGifAnimation(MemoryStream memoryStream)
   {
   Reset();
  
   byte[] gifData = memoryStream.GetBuffer();
  
   GifBitmapDecoder decoder = new GifBitmapDecoder(memoryStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
  
   numberOfFrames = decoder.Frames.Count;
  
   try
   {
   ParseGif(gifData);
   }
   catch
   {
   throw new FileFormatException("Unable to parse Gif file format.");
   }
  
   for (int i = 0; i < decoder.Frames.Count; i++)
   {
   frameList[i].Source = decoder.Frames[i];
   frameList[i].Visibility = Visibility.Hidden;
   canvas.Children.Add(frameList[i]);
   Canvas.SetLeft(frameList[i], frameList[i].left);
   Canvas.SetTop(frameList[i], frameList[i].top);
   Canvas.SetZIndex(frameList[i], i);
   }
   canvas.Height = logicalHeight;
   canvas.Width = logicalWidth;
  
   frameList[0].Visibility = Visibility.Visible;
  
   for (int i = 0; i < frameList.Count; i++)
   {
   Console.WriteLine(frameList[i].disposalMethod.ToString() + " " + frameList[i].width.ToString() + " " + frameList[i].delayTime.ToString());
   }
  
   if (frameList.Count > 1)
   {
   if (numberOfLoops == -1)
   {
   numberOfLoops = 1;
   }
   frameTimer = new System.Windows.Threading.DispatcherTimer();
   frameTimer.Tick += NextFrame;
   frameTimer.Interval = new TimeSpan(0, 0, 0, 0, frameList[0].delayTime * 10);
   frameTimer.Start();
   }
   }
  OK,我们可以像使用Image控件一样来使用我们的GifImage控件了:
   
    

(编辑:IT资讯之家 www.it55.com

返回顶部
共2页: 上一页 [1] 2 下一页  

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]