当前位置:   article > 正文

WPF播放GIF控件完整代码_wpf image 控制gif图片是否播放

wpf image 控制gif图片是否播放

WPF拥有很强的界面设计能力,可以很方便的做出非常漂亮的界面。但有个问题,WPF没有自己的播放GIF的控件。这让很多想在界面播放动态动画的人不得不使用视频来代替。WPF就真的不能播放GIF动图了吗?当然是可以播放的,只是我们需要写一些代码。下面是我总结后写的一个WPF播放GIF动画的控件。

下面介绍一下WPF播放GIF控件思路:

在WinForm里面System.Drawing.Bitmap是可以通过ImageAnimator类来播放GIF动画的,但是可惜的是System.Drawing.Bitmap没有办法直接显示在WPF界面上。于是我们利用ImageAnimator播放System.Drawing.Bitmap对应的GIF动画。在每次System.Drawing.Bitmap上显示的图像改变时,获取当前图像,然后将图像转换为可以在WPF上显示BitmapSource,将转换得来的BitmapSource作为一帧动画显示在界面。

首先我们建立一个命名为GifImage的类,这个类继承自System.Windows.Controls.Image。它的作用就是将System.Drawing.Bitmap转换后得到的BitmapSource赋值给继承自Image控件的Source属性,来显示GIF图像,当GIF每一帧的图像改变时,GifImage显示的内容也会改变,这样就达到了动态效果。下面是代码,很简单。

  1. public class GifImage : System.Windows.Controls.Image
  2. {
  3. /// <summary>
  4. /// gif动画的System.Drawing.Bitmap
  5. /// </summary>
  6. private Bitmap gifBitmap;
  7. /// <summary>
  8. /// 用于显示每一帧的BitmapSource
  9. /// </summary>
  10. private BitmapSource bitmapSource;
  11. public GifImage(string gifPath)
  12. {
  13. this.gifBitmap = new Bitmap(gifPath);
  14. this.bitmapSource = this.GetBitmapSource();
  15. this.Source = this.bitmapSource;
  16. }
  17. /// <summary>
  18. /// 从System.Drawing.Bitmap中获得用于显示的那一帧图像的BitmapSource
  19. /// </summary>
  20. /// <returns></returns>
  21. private BitmapSource GetBitmapSource()
  22. {
  23. IntPtr handle = IntPtr.Zero;
  24. try
  25. {
  26. handle = this.gifBitmap.GetHbitmap();
  27. this.bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  28. }
  29. finally
  30. {
  31. if (handle != IntPtr.Zero)
  32. {
  33. DeleteObject(handle);
  34. }
  35. }
  36. return this.bitmapSource;
  37. }
  38. /// <summary>
  39. /// Start animation
  40. /// </summary>
  41. public void StartAnimate()
  42. {
  43. ImageAnimator.Animate(this.gifBitmap, this.OnFrameChanged);
  44. }
  45. /// <summary>
  46. /// Stop animation
  47. /// </summary>
  48. public void StopAnimate()
  49. {
  50. ImageAnimator.StopAnimate(this.gifBitmap, this.OnFrameChanged);
  51. }
  52. /// <summary>
  53. /// Event handler for the frame changed
  54. /// </summary>
  55. private void OnFrameChanged(object sender, EventArgs e)
  56. {
  57. Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(()=>
  58. {
  59. ImageAnimator.UpdateFrames(); // 更新到下一帧
  60. if (this.bitmapSource != null)
  61. {
  62. this.bitmapSource.Freeze();
  63. }
  64. Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
  65. this.bitmapSource = this.GetBitmapSource();
  66. Source = this.bitmapSource;
  67. this.InvalidateVisual();
  68. }));
  69. }
  70. /// <summary>
  71. /// Delete local bitmap resource
  72. /// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
  73. /// </summary>
  74. [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  75. [return: MarshalAs(UnmanagedType.Bool)]
  76. static extern bool DeleteObject(IntPtr hObject);
  77. }

其中ImageAnimator类是通过静态方法Animate和StopAnimate开始和结束System.Drawing.Bitmap所对应的GIF动画。这两个函数的第二个参数是一个委托,当前播放GIF动画每一帧图像发生改变的时候,这个委托所指定的函数就会被触发。那么我们就可以在这个委托里将当前显示的System.Drawing.Bitmap图像转换为在WPF上可以显示的BitmapSource,然后赋值给Source属性显示在界面。

下面是在一个Window中使用GIFInage控件。ProgressIndicator.gif是要播放的GIF文件的文件名,在程序运行目录。

  1. public partial class MainWindow : Window
  2. {
  3. private GifImage gifImage;
  4. public MainWindow()
  5. {
  6. InitializeComponent();
  7. this.gifImage = new GifImage("ProgressIndicator.gif");
  8. this.gifImage.Width = 100;
  9. this.gifImage.Height = 100;
  10. this.Content = this.gifImage;
  11. }
  12. private void Window_Loaded(object sender, RoutedEventArgs e)
  13. {
  14. this.gifImage.StartAnimate();
  15. }
  16. }



代码下载地址:http://download.csdn.net/detail/libby1984/9637803



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/273646
推荐阅读
相关标签
  

闽ICP备14008679号