基本信息
源码名称:wpf显示gif动画
源码大小:0.33M
文件格式:.zip
开发语言:C#
更新时间:2018-11-22
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
/****************************** Module Header ******************************\ Module Name: AnimatedGIFControl.cs Project: CSWPFAnimatedGIF Copyright (c) Microsoft Corporation. The CSWPFAnimatedGIF demonstrates how to implement an animated GIF image in WPF. This source is subject to the Microsoft Public License. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. All other rights reserved. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. \***************************************************************************/ using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media.Imaging; using System.Windows.Threading; namespace CSWPFAnimatedGIF { public class AnimatedGIFControl : System.Windows.Controls.Image { private Bitmap _bitmap; // Local bitmap member to cache image resource private BitmapSource _bitmapSource; public delegate void FrameUpdatedEventHandler(); /// <summary> /// Delete local bitmap resource /// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx /// </summary> [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool DeleteObject(IntPtr hObject); /// <summary> /// Override the OnInitialized method /// </summary> protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); this.Loaded = new RoutedEventHandler(AnimatedGIFControl_Loaded); this.Unloaded = new RoutedEventHandler(AnimatedGIFControl_Unloaded); } /// <summary> /// Load the embedded image for the Image.Source /// </summary> void AnimatedGIFControl_Loaded(object sender, RoutedEventArgs e) { // Get GIF image from Resources if (Properties.Resources.ProgressIndicator != null) { _bitmap = Properties.Resources.ProgressIndicator; Width = _bitmap.Width; Height = _bitmap.Height; _bitmapSource = GetBitmapSource(); Source = _bitmapSource; } } /// <summary> /// Close the FileStream to unlock the GIF file /// </summary> private void AnimatedGIFControl_Unloaded(object sender, RoutedEventArgs e) { StopAnimate(); } /// <summary> /// Start animation /// </summary> public void StartAnimate() { ImageAnimator.Animate(_bitmap, OnFrameChanged); } /// <summary> /// Stop animation /// </summary> public void StopAnimate() { ImageAnimator.StopAnimate(_bitmap, OnFrameChanged); } /// <summary> /// Event handler for the frame changed /// </summary> private void OnFrameChanged(object sender, EventArgs e) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, new FrameUpdatedEventHandler(FrameUpdatedCallback)); } private void FrameUpdatedCallback() { ImageAnimator.UpdateFrames(); if (_bitmapSource != null) _bitmapSource.Freeze(); // Convert the bitmap to BitmapSource that can be display in WPF Visual Tree _bitmapSource = GetBitmapSource(); Source = _bitmapSource; InvalidateVisual(); } private BitmapSource GetBitmapSource() { IntPtr handle = IntPtr.Zero; try { handle = _bitmap.GetHbitmap(); _bitmapSource = Imaging.CreateBitmapSourceFromHBitmap( handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { if (handle != IntPtr.Zero) DeleteObject(handle); } return _bitmapSource; } } }