贴代码科技-致力于开发更加适用的应用 2015-02-02 06:58 采纳率: 0%
浏览 2163

使用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
        {
        }
    }

  • 写回答

5条回答 默认 最新

  • 关注

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

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

    评论

报告相同问题?