|
| 1 | +using CookPopularCSharpToolkit.Windows.Interop; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Drawing; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows; |
| 11 | +using System.Windows.Interop; |
| 12 | +using System.Windows.Media; |
| 13 | +using System.Windows.Media.Imaging; |
| 14 | +using System.Windows.Navigation; |
| 15 | +using System.Windows.Threading; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +/* |
| 20 | + * Description:Gif |
| 21 | + * Author: Chance(a cook of write code) |
| 22 | + * Company: NCATest |
| 23 | + * Create Time:2021-12-24 21:05:57 |
| 24 | + * .NET Version: 4.6 |
| 25 | + * CLR Version: 4.0.30319.42000 |
| 26 | + * Copyright (c) NCATest 2021 All Rights Reserved. |
| 27 | + */ |
| 28 | +namespace CookPopularControl.Controls |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// GIF动画 |
| 32 | + /// </summary> |
| 33 | + /// <remarks> |
| 34 | + /// https://github.com/XamlAnimatedGif/XamlAnimatedGif |
| 35 | + /// </remarks> |
| 36 | + public class Gif : System.Windows.Controls.Image |
| 37 | + { |
| 38 | + private Bitmap _gifBitmap; |
| 39 | + private BitmapSource _bitmapSource; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gif的路径 |
| 43 | + /// </summary> |
| 44 | + public Uri GifSource |
| 45 | + { |
| 46 | + get { return (Uri)GetValue(GifSourceProperty); } |
| 47 | + set { SetValue(GifSourceProperty, value); } |
| 48 | + } |
| 49 | + /// <summary> |
| 50 | + /// 表示<see cref="GifSource"/>的依赖属性 |
| 51 | + /// </summary> |
| 52 | + public static readonly DependencyProperty GifSourceProperty = |
| 53 | + DependencyProperty.Register("GifSource", typeof(Uri), typeof(Gif), new UIPropertyMetadata(default(Uri), GifSourcePropertyChanged)); |
| 54 | + protected static void GifSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) |
| 55 | + { |
| 56 | + if (obj is Gif gif) |
| 57 | + { |
| 58 | + gif.GifSource = e.NewValue as Uri; |
| 59 | + var stream = Application.GetResourceStream(gif.GifSource).Stream; |
| 60 | + gif._gifBitmap = new Bitmap(stream); |
| 61 | + gif._bitmapSource = gif.GetBitmapSource(); |
| 62 | + gif.Source = gif._bitmapSource; |
| 63 | + gif.StartAnimate(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public void StartAnimate() |
| 68 | + { |
| 69 | + ImageAnimator.Animate(this._gifBitmap, this.OnFrameChanged); |
| 70 | + } |
| 71 | + |
| 72 | + public void StopAnimate() |
| 73 | + { |
| 74 | + ImageAnimator.StopAnimate(this._gifBitmap, this.OnFrameChanged); |
| 75 | + } |
| 76 | + |
| 77 | + private void OnFrameChanged(object sender, EventArgs e) |
| 78 | + { |
| 79 | + Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => |
| 80 | + { |
| 81 | + ImageAnimator.UpdateFrames(); |
| 82 | + if (this._bitmapSource != null) |
| 83 | + { |
| 84 | + this._bitmapSource.Freeze(); |
| 85 | + } |
| 86 | + |
| 87 | + this._bitmapSource = this.GetBitmapSource(); |
| 88 | + Source = this._bitmapSource; |
| 89 | + this.InvalidateVisual(); |
| 90 | + })); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// BitmapSource用于显示System.drawing.bitmap中的图像的帧 |
| 95 | + /// </summary> |
| 96 | + /// <returns></returns> |
| 97 | + private BitmapSource GetBitmapSource() |
| 98 | + { |
| 99 | + IntPtr handle = IntPtr.Zero; |
| 100 | + |
| 101 | + try |
| 102 | + { |
| 103 | + handle = this._gifBitmap.GetHbitmap(); |
| 104 | + this._bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); |
| 105 | + } |
| 106 | + finally |
| 107 | + { |
| 108 | + if (handle != IntPtr.Zero) |
| 109 | + { |
| 110 | + InteropMethods.DeleteObject(handle); |
| 111 | + } |
| 112 | + } |
| 113 | + return this._bitmapSource; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments