最近碰到了要顯示錶情的需求,而表情恰好是gif的圖片。html
因而用了Image試了下,發現不行,只會顯示第一幀,而後上網查了下資料,大體有這麼幾種方法,均可以實現。測試
第一種:動畫
使用Winfrom裏面的picturebox,缺點是要引用幾個winfrom的dllui
第二種:this
用wpf的mediaelement控件,這控件自己是用來顯示視頻的,可是能夠拿來放gif,spa
這種方式有一個侷限就是圖片路徑必須是絕對路徑code
<MediaElement Source="file://C:\129.gif" />
而且你還須要設置讓他循環播放orm
<MediaElement Source="file://C:\129.gif" MediaEnded="MediaElement_MediaEnded"/> private void MediaElement_MediaEnded(object sender, RoutedEventArgs e) { ((MediaElement)sender).Position=((MediaElement)sender).Position.Add(TimeSpan.FromMilliseconds(1)); }
並且我發現這種方法在win7以上的系統中才能使用,在XP系統下就失效了,因此果斷放棄
來自周銀輝的博客,原文地址:http://www.cnblogs.com/zhouyinhui/archive/2007/12/23/1011555.html視頻
大體的思路是:使用GifBitmapDecoder類,其能夠將動態GIF分解成不少幀並保存在一個列表中,每一幀爲一個BitmapFrame類型的對象,其父類爲BitmapSource,能夠將每一幀賦值給一個Image控件的Source屬性,這樣能夠獲得針對GIF各幀的Image系列。htm
第四種:就是目前我在用的方法,重寫下Wpf的Image控件
來自CH似水年華的博客,原文地址: http://hi.baidu.com/mych/blog/item/1eb14f545f12a752564e00be.html
首先新建一個類繼承自Image
public class GifImage : System.Windows.Controls.Image { /// <summary> /// gif動畫的System.Drawing.Bitmap /// </summary> private Bitmap gifBitmap; /// <summary> /// 用於顯示每一幀的BitmapSource /// </summary> private BitmapSource bitmapSource; public GifImage(string gifPath) { this.gifBitmap = new Bitmap(gifPath); this.bitmapSource = this.GetBitmapSource(); this.Source = this.bitmapSource; } /// <summary> /// 從System.Drawing.Bitmap中得到用於顯示的那一幀圖像的BitmapSource /// </summary> /// <returns></returns> private BitmapSource GetBitmapSource() { IntPtr handle = IntPtr.Zero; try { handle = this.gifBitmap.GetHbitmap(); this.bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { if (handle != IntPtr.Zero) { DeleteObject(handle); } } return this.bitmapSource; } /// <summary> /// Start animation /// </summary> public void StartAnimate() { ImageAnimator.Animate(this.gifBitmap, this.OnFrameChanged); } /// <summary> /// Stop animation /// </summary> public void StopAnimate() { ImageAnimator.StopAnimate(this.gifBitmap, this.OnFrameChanged); } /// <summary> /// Event handler for the frame changed /// </summary> private void OnFrameChanged(object sender, EventArgs e) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { ImageAnimator.UpdateFrames(); // 更新到下一幀 if (this.bitmapSource != null) { this.bitmapSource.Freeze(); } //// Convert the bitmap to BitmapSource that can be display in WPF Visual Tree this.bitmapSource = this.GetBitmapSource(); Source = this.bitmapSource; this.InvalidateVisual(); })); } /// <summary> /// Delete local bitmap resource /// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx /// </summary> [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool DeleteObject(IntPtr hObject); }而後調用方式以下:
private GifImage gifImage; public MainWindow() { InitializeComponent(); this.gifImage = new GifImage("ProgressIndicator.gif"); this.gifImage.Width = 100; this.gifImage.Height = 100; this.Content = this.gifImage; } private void Window_Loaded(object sender, RoutedEventArgs e) { Popup dd = new Popup(); this.gifImage.StartAnimate(); }經測試,能夠在xp下完美顯示