WPF:使用GifBitmapDecoder解析GIF動畫檔案

來源:互聯網
上載者:User

感謝WPF的強大封裝,這一切竟然3段代碼就完成了。

比如我們常見到的那個兔子揉臉的GIF:

 

程式運行結果:

 

代碼則是使用BitmapDecoder類型,針對GIF動畫,則需要初始化它的子類GifBitmapDecoder。而BitmapDecoder得Frames屬性則包含一系列的BitmapFrame。這個類型是BitmapDecoder(和BitmapEncoder)中用到的映像幀,繼承自BitmapSource,因此本質上也是一種位元影像類型。

 

XAML:

<ListBox Name="lbx">

    <ListBox.ItemsPanel>

        <ItemsPanelTemplate>

            <WrapPanel/>

        </ItemsPanelTemplate>

    </ListBox.ItemsPanel>

    <ListBox.ItemTemplate>

        <DataTemplate>

            <Image Source="{Binding}"/>

        </DataTemplate>

    </ListBox.ItemTemplate>

</ListBox>

 

代碼:

var uri = new Uri(@"C:\Users\Mgen\Desktop\a.gif");

var gifDecoder = new GifBitmapDecoder(uri, BitmapCreateOptions.None, BitmapCacheOption.Default);

lbx.ItemsSource = gifDecoder.Frames;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.