我正在尝试在Windows Phone 8.1 (RT)应用程序的本地文件夹中显示一个GIF文件。到目前为止,我已经尝试了几种方法:
问题是我从GIF图像中获取的帧混乱了。
我在这个方法中做错了什么吗?我应该修改什么才能得到好的结果?
我不想使用SharpDX,因为它似乎非常复杂,而且我是初学者。
ImageTools仅适用于Silverlight。
使用WebView控件加载GIF。这不是一个解决方案,因为我想在FlipView控件中显示图像(我无法翻转到另一张图像)。而且它非常慢。
使用BitmapDecoder类获取GIF帧,并使用故事板将它们动画化。
代码如下:
using (var stream = await storageFile.OpenReadAsync())
{
//I don't specify the type since I would like to load other type of images also
//It doesn't make any difference if I would use "BitmapDecoder.GifDecoderId"
var decoder = await BitmapDecoder.CreateAsync(stream);
uint frameCount = decoder.FrameCount;
for (uint frameIndex = 0; frameIndex < frameCount; frameIndex++)
{
var frame = await decoder.GetFrameAsync(frameIndex);
var writableBitmap = new WriteableBitmap((int)decoder.OrientedPixelWidth,
(int)decoder.OrientedPixelHeight);
var pixelDataProvider = await frame.GetPixelDataAsync
(BitmapPixelFormat.Bgra8,
decoder.BitmapAlphaMode, new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
var pixelData = pixelDataProvider.DetachPixelData();
using (var pixelStream = writableBitmap.PixelBuffer.AsStream())
{
pixelStream.Write(pixelData, 0, pixelData.Length);
}
_bitmapFrames.Add(writableBitmap);
}
}
问题是我从GIF图像中获取的帧混乱了。
![like this one](https://istack.dev59.com/08eeH.webp)
我不想使用SharpDX,因为它似乎非常复杂,而且我是初学者。
ImageTools仅适用于Silverlight。
<MediaElement Source="YourAnimated.gif"/>
。不过,如果你想保留它,请告诉我,这样我就可以将其作为答案,以便能够轻松获得一些点数。 :) - Chris W.AreTransportControlsEnabled
设置为true后,我看到了错误提示“不支持的视频类型或无效的文件路径”。此外,GIF文件的路径已正确设置。 - rhcpfan