ImageAnimator.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // System.Drawing.ImageAnimator.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (C) 2002 Ximian, Inc
  10. // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
  11. //
  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:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  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.
  30. //
  31. using System.Collections;
  32. using System.Drawing.Imaging;
  33. using System.Threading;
  34. namespace System.Drawing {
  35. class AnimateEventArgs : EventArgs {
  36. private int frameCount;
  37. private int activeFrame;
  38. private Thread thread;
  39. public AnimateEventArgs (Image image)
  40. {
  41. frameCount = image.GetFrameCount (FrameDimension.Time);
  42. }
  43. public Thread RunThread {
  44. get { return thread; }
  45. set { thread = value; }
  46. }
  47. public int GetNextFrame ()
  48. {
  49. if (activeFrame < frameCount - 1)
  50. activeFrame++;
  51. else
  52. activeFrame = 0;
  53. return activeFrame;
  54. }
  55. }
  56. public sealed class ImageAnimator {
  57. static Hashtable ht = Hashtable.Synchronized (new Hashtable ());
  58. private ImageAnimator ()
  59. {
  60. }
  61. public static void Animate (Image image, EventHandler onFrameChangeHandler)
  62. {
  63. // must be non-null and contain animation time frames
  64. if (!CanAnimate (image))
  65. return;
  66. // is animation already in progress ?
  67. if (ht.ContainsKey (image))
  68. return;
  69. PropertyItem item = image.GetPropertyItem (0x5100); // FrameDelay in libgdiplus
  70. byte[] value = item.Value;
  71. int[] delay = new int [(value.Length >> 2)];
  72. for (int i=0, n=0; i < value.Length; i += 4, n++) {
  73. int d = BitConverter.ToInt32 (value, i) * 10;
  74. // follow worse case (Opera) see http://news.deviantart.com/article/27613/
  75. delay [n] = d < 100 ? 100 : d;
  76. }
  77. AnimateEventArgs aea = new AnimateEventArgs (image);
  78. WorkerThread wt = new WorkerThread (onFrameChangeHandler, aea, delay);
  79. Thread thread = new Thread (new ThreadStart (wt.LoopHandler));
  80. thread.IsBackground = true;
  81. aea.RunThread = thread;
  82. ht.Add (image, aea);
  83. thread.Start ();
  84. }
  85. public static bool CanAnimate (Image image)
  86. {
  87. if (image == null)
  88. return false;
  89. int n = image.FrameDimensionsList.Length;
  90. if (n < 1)
  91. return false;
  92. for (int i = 0; i < n; i++) {
  93. if (image.FrameDimensionsList [i].Equals (FrameDimension.Time.Guid)) {
  94. return (image.GetFrameCount (FrameDimension.Time) > 1);
  95. }
  96. }
  97. return false;
  98. }
  99. public static void StopAnimate (Image image, EventHandler onFrameChangeHandler)
  100. {
  101. if (image == null)
  102. return;
  103. if (ht.ContainsKey (image)) {
  104. AnimateEventArgs evtArgs = (AnimateEventArgs) ht [image];
  105. evtArgs.RunThread.Abort ();
  106. ht.Remove (image);
  107. }
  108. }
  109. public static void UpdateFrames ()
  110. {
  111. foreach (Image image in ht.Keys)
  112. UpdateImageFrame (image);
  113. }
  114. public static void UpdateFrames (Image image)
  115. {
  116. if (image == null)
  117. return;
  118. if (ht.ContainsKey (image))
  119. UpdateImageFrame (image);
  120. }
  121. // this method avoid checks that aren't requied for UpdateFrames()
  122. private static void UpdateImageFrame (Image image)
  123. {
  124. AnimateEventArgs aea = (AnimateEventArgs) ht [image];
  125. image.SelectActiveFrame (FrameDimension.Time, aea.GetNextFrame ());
  126. }
  127. }
  128. class WorkerThread {
  129. private EventHandler frameChangeHandler;
  130. private AnimateEventArgs animateEventArgs;
  131. private int[] delay;
  132. public WorkerThread (EventHandler frmChgHandler, AnimateEventArgs aniEvtArgs, int[] delay)
  133. {
  134. frameChangeHandler = frmChgHandler;
  135. animateEventArgs = aniEvtArgs;
  136. this.delay = delay;
  137. }
  138. public void LoopHandler ()
  139. {
  140. try {
  141. int n = 0;
  142. while (true) {
  143. Thread.Sleep (delay [n++]);
  144. frameChangeHandler (null, animateEventArgs);
  145. if (n == delay.Length)
  146. n = 0;
  147. }
  148. }
  149. catch (ThreadAbortException) {
  150. Thread.ResetAbort (); // we're going to finish anyway
  151. }
  152. }
  153. }
  154. }