使用BitmapDecoder读取GIF图片的问题 Win8.1 WP8.1 appx

    private string filename = "8.gif";

    private List<WriteableBitmap> list = new List<WriteableBitmap>();

    async void hf_FileCompeted(int code, object sender)
    {

        Windows.Storage.StorageFolder localFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("image", Windows.Storage.CreationCollisionOption.OpenIfExists);
        IStorageFile fileinto = await localFolder.GetFileAsync(filename);
        IRandomAccessStream ias = await fileinto.OpenReadAsync();
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, ias);

        System.Diagnostics.Debug.WriteLine("总的帧数:" + decoder.FrameCount.ToString());

        Print(decoder.PixelHeight + ":" + decoder.PixelWidth);


        for (int k = 0; k < (int)decoder.FrameCount; k++)
        {
            // 获取第一帧
            BitmapFrame frame = await decoder.GetFrameAsync((uint)k);
            // 获取像素数据
            PixelDataProvider pixprd = await frame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

            Print(frame.DpiX+":"+frame.DpiY+":"+frame.OrientedPixelHeight+":"+frame.OrientedPixelWidth+":"+frame.PixelHeight+":"+frame.PixelWidth);


            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                // 创建新的位图对象
                WriteableBitmap wb = new WriteableBitmap((int)frame.PixelWidth, (int)frame.PixelHeight);
                byte[] data = pixprd.DetachPixelData();

                data.CopyTo(wb.PixelBuffer);
                wb.Invalidate();
                list.Add(wb);
            }
            );

        }

        time = new System.Threading.Timer(new TimerCallback(timerReport), null, 300, 0);

    }

    private void Print(string str) 
    {
        System.Diagnostics.Debug.WriteLine(str);
    }


    System.Threading.Timer time;
    private int kindex = 0;
    /// <summary>
    /// 计时器检查链接和心跳包
    /// </summary>
    /// <param name="obj"></param>
    private async void timerReport(object obj)
    {
        try
        {
            time.Change(300, 0);
            kindex++;
            if (kindex >= 8)
            {
                kindex = 0;
            }


            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                img.Source = list[kindex];
            });


        }
        catch
        {
        }
    }

问题在于,图片背景是白色的!!!

应是透明的撒,怎么搞? 谢谢!

代码的意思是,从存储区读取文件,然后通过那个BitmapDecoder读取图片的信息,获得gif的帧数和每一帧的数据,然后存在一个list里面,最后通过时间切换,这是一个简单的例子。很多东西没有附加

    /// <summary>
    /// 文件的后缀名
    /// </summary>
    private string filename = "12.gif";
    /// <summary>
    /// 帧信息的组
    /// </summary>
    private List<Image> list = new List<Image>();
    /// <summary>
    /// 共有多少帧
    /// </summary>
    private int framecount = 0;

    /// <summary>
    /// 文件下载完成后
    /// </summary>
    /// <param name="code"></param>
    /// <param name="sender"></param>
    private async void hf_FileCompeted(int code, object sender)
    {

        Windows.Storage.StorageFolder localFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("image", Windows.Storage.CreationCollisionOption.OpenIfExists);
        IStorageFile fileinto = await localFolder.GetFileAsync(filename);
        IRandomAccessStream ias = await fileinto.OpenReadAsync();
        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, ias);

        framecount = (int)decoder.FrameCount;
        System.Diagnostics.Debug.WriteLine("总的帧数:" + decoder.FrameCount.ToString());

        Print(decoder.PixelHeight + ":" + decoder.PixelWidth);

        for (int k = 0; k < (int)decoder.FrameCount; k++)
        {

            BitmapFrame frame = await decoder.GetFrameAsync((uint)k);

            var frameProperties = await frame.BitmapProperties.GetPropertiesAsync(new List<string>());

            var imageDescriptionProperties = await (frameProperties["/imgdesc"].Value as BitmapPropertiesView).GetPropertiesAsync(new List<string>() { "/Top", "/Left", "/Width", "/Height" });
            int top = Int32.Parse(imageDescriptionProperties["/Top"].Value.ToString());
            int left = Int32.Parse(imageDescriptionProperties["/Left"].Value.ToString());
            int width = Int32.Parse(imageDescriptionProperties["/Width"].Value.ToString());
            int height = Int32.Parse(imageDescriptionProperties["/Height"].Value.ToString());

            var gifControlExtensionProperties = await (frameProperties["/grctlext"].Value as BitmapPropertiesView).GetPropertiesAsync(new List<string>() { "/Delay", "/UserInputFlag" });
            TimeSpan delay = TimeSpan.FromSeconds(Double.Parse(gifControlExtensionProperties["/Delay"].Value.ToString()) / 100); // delay is in 1/100s of a second
            bool userInputFlag = bool.Parse(gifControlExtensionProperties["/UserInputFlag"].Value.ToString());

            Print(top+"::"+left+"::"+width+"::"+height);



            // 获取像素数据
            PixelDataProvider pixprd = await frame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

            Print(frame.DpiX + ":" + frame.DpiY + ":" + frame.OrientedPixelHeight + ":" + frame.OrientedPixelWidth + ":" + frame.PixelHeight + ":" + frame.PixelWidth);


            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                // 创建新的位图对象
                WriteableBitmap wb = new WriteableBitmap((int)frame.PixelWidth, (int)frame.PixelHeight);
                byte[] data = pixprd.DetachPixelData();

                data.CopyTo(wb.PixelBuffer);
                wb.Invalidate();



                Image img = new Image();
                img.Width = width * 2;
                img.Height = height * 2;
                img.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
                img.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
                img.Margin = new Thickness(left * 2, top * 2, 0, 0);
                img.Source = wb;
                Canvas.SetZIndex(img, k);

                list.Add(img);

                can.Children.Add(img);
            }
            );

        }


        //for (int k = 0; k < (int)decoder.FrameCount; k++)
        //{
        //    // 获取第一帧
        //    BitmapFrame frame = await decoder.GetFrameAsync((uint)k);
        //    // 获取像素数据
        //    PixelDataProvider pixprd = await frame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

        //    Print(frame.DpiX+":"+frame.DpiY+":"+frame.OrientedPixelHeight+":"+frame.OrientedPixelWidth+":"+frame.PixelHeight+":"+frame.PixelWidth);


        //    var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
        //    await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        //    {
        //        // 创建新的位图对象
        //        WriteableBitmap wb = new WriteableBitmap((int)frame.PixelWidth, (int)frame.PixelHeight);
        //        byte[] data = pixprd.DetachPixelData();

        //        data.CopyTo(wb.PixelBuffer);
        //        wb.Invalidate();
        //        list.Add(wb);
        //    }
        //    );

        //}

        time = new System.Threading.Timer(new TimerCallback(timerReport), null, 300, 0);

    }

    private void Print(string str) 
    {
        System.Diagnostics.Debug.WriteLine(str);
    }


    System.Threading.Timer time;
    /// <summary>
    /// 当前显示第几帧
    /// </summary>
    private int kindex = 0;
    /// <summary>
    /// 计时器检查链接和心跳包
    /// </summary>
    /// <param name="obj"></param>
    private async void timerReport(object obj)
    {
        try
        {
            time.Change(200, 0);
            kindex++;
            if (kindex >= framecount)
            {
                kindex = 0;
            }


            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {

                Canvas.SetZIndex(list[kindex],framecount);
                if (kindex == 0)
                {
                    Canvas.SetZIndex(list[framecount - 1], 0);
                }
                else 
                {
                    Canvas.SetZIndex(list[kindex-1],0);
                }
            });
        }
        catch
        {
        }
    }

就差背景色是白色的问题了,没人来么?

兄弟,印象中gif不支持透明,png才支持,找点资料确认一下,实在不行,自己实现一个gif,用一个timer,多帧循环播。