■ Image 클래스를 사용해 움직이는 GIF 이미지를 만드는 방법을 보여준다.
▶ GIFImage.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
using System; using System.Windows.Media.Imaging; using System.Windows; using System.Windows.Controls; namespace TestProject; /// <summary> /// GIF 이미지 /// </summary> public class GIFImage : Image { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 디코더 /// </summary> private BitmapDecoder decoder = null; /// <summary> /// 타이머 /// </summary> private System.Timers.Timer timer = new System.Timers.Timer(); /// <summary> /// 타이머 잠금 /// </summary> private object timerLock = new object(); /// <summary> /// 프레임 카운트 /// </summary> private int frameCount = 0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 타이머 주기 - Interval /// <summary> /// 타이머 주기 /// </summary> public double Interval { get { return this.timer.Interval; } set { this.timer.Interval = value; } } #endregion #region 재생 여부 - IsPlaying /// <summary> /// 재생 여부 /// </summary> public bool IsPlaying { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - GIFImage() /// <summary> /// 생성자 /// </summary> public GIFImage() : base() { IsPlaying = false; Interval = 100; this.timer.Elapsed += timer_Elapsed; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 시작하기 - Start() /// <summary> /// 시작하기 /// </summary> public void Start() { lock(this.timerLock) { if(this.decoder != null && this.decoder.Frames.Count > 1) { if(IsPlaying == false) { IsPlaying = true; this.timer.Start(); } } } } #endregion #region 중단하기 - Stop() /// <summary> /// 중단하기 /// </summary> public void Stop() { lock(this.timerLock) { IsPlaying = false; this.timer.Stop(); } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 처리하기 - OnPropertyChanged(e) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { if(e.Property.Name == "Source") { if(e.NewValue != null && (e.OldValue == null || (e.NewValue.ToString() != e.OldValue.ToString()))) { Stop(); try { GifBitmapDecoder decoder = new GifBitmapDecoder(new Uri(e.NewValue.ToString()), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); this.decoder = decoder; } catch { } Start(); } } base.OnPropertyChanged(e); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 타이머 경과시 처리하기 - timer_Elapsed(sender, e) /// <summary> /// 타이머 경과시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Dispatcher.BeginInvoke ( new Action ( delegate { this.frameCount = ++this.frameCount % this.decoder.Frames.Count; Source = this.decoder.Frames[this.frameCount]; InvalidateArrange(); } ), null ); } #endregion } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <local:GIFImage HorizontalAlignment="Center" SnapsToDevicePixels="True" UseLayoutRounding="True" RenderOptions.BitmapScalingMode="NearestNeighbor" Stretch="None" Interval="100" Source="pack://application:,,,/IMAGE/elephant.gif" /> </StackPanel> </Window> |