在图像控件中显示GIF框架

问题描述 投票:2回答:1

我尝试在Image控件内显示一个加载的gif图像(来自文件)的单帧。我在Image控件中显示(完整)gif图像没有问题,但我无法显示框架。出于测试目的,我总是尝试加载帧'0'(第一帧)。

XAML:

<Image>
    <Image.Source>
        <BitmapImage x:Name="GifFrame" AutoPlay="False" />
    </Image.Source>
</Image>

我真的没有在网上找到关于如何做到这一点的大量信息,但是这里有一些代码片段,有些可能不幸来自WPF,我不能100%确定它们是否也可以用于UWP。

打开图像时,将调用以下“_OnImageOpened”方法(_gifStream是打开的gif图像,流尚未关闭):

private IRandomAccessStream _gifStream = null;

private async void GifImage_OnImageOpened(object sender, RoutedEventArgs e)
{
    // ...

    uint frameCount = 0;
    if (_gifStream != null)
    {
        var decoder = await BitmapDecoder.CreateAsync(_gifStream);
        frameCount = decoder.FrameCount;

        var frame = await decoder.GetFrameAsync(0);

        // Create frame image
        var pixelData = await frame.GetPixelDataAsync(
            BitmapPixelFormat.Bgra8,
            BitmapAlphaMode.Premultiplied,
            new BitmapTransform(),
            ExifOrientationMode.IgnoreExifOrientation,
            ColorManagementMode.DoNotColorManage
            ).AsTask().ConfigureAwait(false);
        var frameBytes = pixelData.DetachPixelData();
        var memoryStream = new MemoryStream(frameBytes);
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                GifFrame.SetSource(memoryStream.AsRandomAccessStream());
            });
    }
}

当我设置新的源(GifFrame.SetSource(...))时,窗口冻结并且没有任何反应,我必须终止该过程。在我在新源的设置周围添加Dispatcher.RunAsync(...)之前,我得到了一个异常“应用程序称为为不同线程编组的接口......”。

我不知道是什么原因引起的。也许我需要将字节转换为图像控件可以理解的东西,或者整个像素数据创建可能是错误的?

c# uwp uwp-xaml animated-gif memorystream
1个回答
1
投票

这是实现您所需要的一种方式:

XAML:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <StackPanel>
            <Button Content="Open" Click="Button_Click" />
            <Image x:Name="Image" Width="100" Height="100" />
        </StackPanel>
    </Grid>
</Page>

码:

using System;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media.Imaging;

namespace App1
{
    public sealed partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var picker = new FileOpenPicker();

            picker.FileTypeFilter.Add(".gif");

            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

            var file = await picker.PickSingleFileAsync();
            if (file == null)
                return;

            var stream = await file.OpenAsync(FileAccessMode.Read);

            var decoder = await BitmapDecoder.CreateAsync(stream);

            var frame = await decoder.GetFrameAsync(0);

            var softwareBitmap = await frame.GetSoftwareBitmapAsync();

            var convert = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

            var softwareBitmapSource = new SoftwareBitmapSource();

            await softwareBitmapSource.SetBitmapAsync(convert);

            Image.Source = softwareBitmapSource;
        }
    }
}

结果:

enter image description here

相当复杂的过程至少可以说......

原创GIF:

enter image description here

您的实际错误是您尝试使用原始像素数据流来提供SetSource,这不是what it expects

© www.soinside.com 2019 - 2024. All rights reserved.