注:本工程来codeproject
WPF默认的IMAGE是无法让GIF文件动起来的。
下面为一种可以让GIF在WPF中动起来的办法。
关键还在AnimatedGIFControl.cs文件中。
-
/****************************** 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
-
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();
-
-
-
///
-
/// Delete local bitmap resource
-
/// Reference: (VS.85).aspx
-
///
-
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
-
static extern bool DeleteObject(IntPtr hObject);
-
-
///
-
/// Override the OnInitialized method
-
///
-
protected override void OnInitialized(EventArgs e)
-
{
-
base.OnInitialized(e);
-
this.Loaded += new RoutedEventHandler(AnimatedGIFControl_Loaded);
-
this.Unloaded += new RoutedEventHandler(AnimatedGIFControl_Unloaded);
-
}
-
-
///
-
/// Load the embedded image for the Image.Source
-
///
-
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;
-
}
-
}
-
-
///
-
/// Close the FileStream to unlock the GIF file
-
///
-
private void AnimatedGIFControl_Unloaded(object sender, RoutedEventArgs e)
-
{
-
StopAnimate();
-
}
-
-
///
-
/// Start animation
-
///
-
public void StartAnimate()
-
{
-
ImageAnimator.Animate(_bitmap, OnFrameChanged);
-
}
-
-
///
-
/// Stop animation
-
///
-
public void StopAnimate()
-
{
-
ImageAnimator.StopAnimate(_bitmap, OnFrameChanged);
-
}
-
-
///
-
/// Event handler for the frame changed
-
///
-
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;
-
}
-
-
}
-
}
测试工程代码:
Show GIF animation in WPF (CSWPFAnimatedGIF).rar
阅读(3325) | 评论(0) | 转发(0) |