2 // System.Drawing.ImageAnimator.cs
5 // Dennis Hayes (dennish@Raytek.com)
6 // Sanjay Gupta (gsanjay@novell.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
;
33 using System
.Drawing
.Imaging
;
34 using System
.Threading
;
36 namespace System
.Drawing
{
38 class AnimateEventArgs
: EventArgs
{
40 private int frameCount
;
41 private int activeFrame
;
42 private Thread thread
;
44 public AnimateEventArgs (Image image
)
46 frameCount
= image
.GetFrameCount (FrameDimension
.Time
);
49 public Thread RunThread
{
50 get { return thread; }
51 set { thread = value; }
54 public int GetNextFrame ()
56 if (activeFrame
< frameCount
- 1)
65 public sealed class ImageAnimator
{
67 static Hashtable ht
= Hashtable
.Synchronized (new Hashtable ());
69 private ImageAnimator ()
73 public static void Animate (Image image
, EventHandler onFrameChangedHandler
)
75 // must be non-null and contain animation time frames
76 if (!CanAnimate (image
))
79 // is animation already in progress ?
80 if (ht
.ContainsKey (image
))
83 PropertyItem item
= image
.GetPropertyItem (0x5100); // FrameDelay in libgdiplus
84 byte[] value = item
.Value
;
85 int[] delay
= new int [(value.Length
>> 2)];
86 for (int i
=0, n
=0; i
< value.Length
; i
+= 4, n
++) {
87 int d
= BitConverter
.ToInt32 (value, i
) * 10;
88 // follow worse case (Opera) see http://news.deviantart.com/article/27613/
89 delay
[n
] = d
< 100 ? 100 : d
;
92 AnimateEventArgs aea
= new AnimateEventArgs (image
);
93 WorkerThread wt
= new WorkerThread (onFrameChangedHandler
, aea
, delay
);
94 Thread thread
= new Thread (new ThreadStart (wt
.LoopHandler
));
95 thread
.IsBackground
= true;
96 aea
.RunThread
= thread
;
101 public static bool CanAnimate (Image image
)
106 int n
= image
.FrameDimensionsList
.Length
;
110 for (int i
= 0; i
< n
; i
++) {
111 if (image
.FrameDimensionsList
[i
].Equals (FrameDimension
.Time
.Guid
)) {
112 return (image
.GetFrameCount (FrameDimension
.Time
) > 1);
118 public static void StopAnimate (Image image
, EventHandler onFrameChangedHandler
)
123 if (ht
.ContainsKey (image
)) {
124 AnimateEventArgs evtArgs
= (AnimateEventArgs
) ht
[image
];
125 evtArgs
.RunThread
.Abort ();
130 public static void UpdateFrames ()
132 foreach (Image image
in ht
.Keys
)
133 UpdateImageFrame (image
);
137 public static void UpdateFrames (Image image
)
142 if (ht
.ContainsKey (image
))
143 UpdateImageFrame (image
);
146 // this method avoid checks that aren't requied for UpdateFrames()
147 private static void UpdateImageFrame (Image image
)
149 AnimateEventArgs aea
= (AnimateEventArgs
) ht
[image
];
150 image
.SelectActiveFrame (FrameDimension
.Time
, aea
.GetNextFrame ());
156 private EventHandler frameChangeHandler
;
157 private AnimateEventArgs animateEventArgs
;
160 public WorkerThread (EventHandler frmChgHandler
, AnimateEventArgs aniEvtArgs
, int[] delay
)
162 frameChangeHandler
= frmChgHandler
;
163 animateEventArgs
= aniEvtArgs
;
167 public void LoopHandler ()
172 Thread
.Sleep (delay
[n
++]);
173 frameChangeHandler (null, animateEventArgs
);
174 if (n
== delay
.Length
)
178 catch (ThreadAbortException
) {
179 Thread
.ResetAbort (); // we're going to finish anyway