在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所有帧,然后存入集合,循环播放,
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值