1. 程式人生 > >在Wpf中使用動態GIF影象的簡單方法

在Wpf中使用動態GIF影象的簡單方法

Wpf本身的方法如果想載入GIF影象實在是過於複雜,我們可以通過使用winform控制元件,減少複雜性

方法一:利用winform控制元件
  System.Windows.Forms. PictureBox pb = new System.Windows.Forms.PictureBox() 
         {
           Width = 200, Height = 150, SizeMode = PictureBoxSizeMode .CenterImage
           };
            pb.Image = System.Drawing. Image.FromFile
(@"C:\Users\zez\Desktop\201203161640295.gif" ); System.Windows.Forms.Integration. WindowsFormsHost wfh = new System.Windows.Forms.Integration.WindowsFormsHost (); wfh.Child = pb; grid.Children.Add(wfh); 注 :System.Windows.Forms.Integration名稱空間在V3.0中,有時需要單獨新增引用 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0
\... 方法二:只能讀取第一幀 BitmapImage img = new BitmapImage(); img.BeginInit(); img.UriSource = new Uri (@"C:\Users\zez\Desktop\201203161640295.gif"); img.DecodePixelWidth = 200; img.EndInit(); Border bor = new Border() { Width=300 ,Height=200 ,HorizontalAlignment= System.Windows
.HorizontalAlignment .Left}; bor.Background = new ImageBrush (img); grid.Children.Add(bor); 方法三:使用解碼器 Uri myUri = new Uri("tulipfarm.bmp", UriKind.RelativeOrAbsolute);//(第二個引數指定Uri地址) GifBitmapDecoder decoder2 = new GifBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 通過decoder2.Frames可以獲取gif所有幀,然後存入集合,迴圈播放,