来源:https://blog.csdn.net/wcc27857285/article/details/52993099
public class GifImage : System.Windows.Controls.Image
{
/// <summary>
/// gif动画的System.Drawing.Bitmap
/// </summary>
private readonly Bitmap gifBitmap;
/// <summary>
/// 用于显示每一帧的BitmapSource
/// </summary>
private BitmapSource bitmapSource;
public GifImage(string gifPath)
{
gifBitmap = new Bitmap(gifPath);
bitmapSource = GetBitmapSource();
Source = bitmapSource;
}
/// <summary>
/// 从System.Drawing.Bitmap中获得用于显示的那一帧图像的BitmapSource
/// </summary>
/// <returns></returns>
private BitmapSource GetBitmapSource()
{
var handle = IntPtr.Zero;
try
{
handle = gifBitmap.GetHbitmap();
bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
if (handle != IntPtr.Zero)
{
DeleteObject(handle);
}
}
return bitmapSource;
}
/// <summary>
/// Start animation
/// </summary>
public void StartAnimate()
{
ImageAnimator.Animate(gifBitmap, OnFrameChanged);
}
/// <summary>
/// Stop animation
/// </summary>
public void StopAnimate()
{
ImageAnimator.StopAnimate(gifBitmap, OnFrameChanged);
}
/// <summary>
/// Event handler for the frame changed
/// </summary>
private void OnFrameChanged(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ImageAnimator.UpdateFrames(); // 更新到下一帧
bitmapSource?.Freeze();
//// Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
bitmapSource = GetBitmapSource();
Source = bitmapSource;
InvalidateVisual();
}));
}
/// <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)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteObject(IntPtr hObject);
}
引用:
private GifImage gifImage;
public MainWindow()
{
InitializeComponent();
this.gifImage = new GifImage("ProgressIndicator.gif");
this.gifImage.Width = 100;
this.gifImage.Height = 100;
this.Content = this.gifImage;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Popup dd = new Popup();
this.gifImage.StartAnimate();
}