PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/graph/tp1/ImageBuilder/ProtoImageBuilder.cs

https://bitbucket.org/dbernard456/cegepa12
C# | 209 lines | 170 code | 37 blank | 2 comment | 6 complexity | 2427b900636fc3e34a43ec0f9aa01236 MD5 | raw file
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. using MediaReader.Time;
  10. namespace MediaReader.ImageBuilder
  11. {
  12. public class ProtoImageBuilder
  13. {
  14. private static readonly int DPI = 96;
  15. private static readonly int TEXT_SIZE = 10;
  16. private static readonly PixelFormat PIXEL_FORMAT = PixelFormats.Pbgra32;
  17. private static readonly CultureInfo CULTURE = CultureInfo.GetCultureInfo("en-us");
  18. private static readonly FlowDirection FLOW_DIRECTION = FlowDirection.LeftToRight;
  19. private static readonly Typeface TYPEFACE = new Typeface("Verdana");
  20. private static readonly SolidColorBrush COLOR = Brushes.Red;
  21. private DrawingVisual drawingVisual;
  22. private MediaElement mediaElement;
  23. private RenderTargetBitmap bmp;
  24. private Image mainImage;
  25. private int currentX;
  26. private int currentY;
  27. private int currentI;
  28. private int currentJ;
  29. private int width;
  30. private int height;
  31. private int totalWidth;
  32. private int totalHeight;
  33. private int columns;
  34. private int lines;
  35. private int nbImages;
  36. private Boolean closed;
  37. private Double scale;
  38. public ProtoImageBuilder(MediaElement element, int parameterLines, int parameterColumns, Double imageScale)
  39. {
  40. this.mediaElement = element;
  41. this.nbImages = 0;
  42. this.closed = false;
  43. this.currentI = 0;
  44. this.currentJ = 0;
  45. this.columns = parameterColumns;
  46. this.lines = parameterLines;
  47. this.scale = imageScale;
  48. this.width = (int)(element.ActualWidth * imageScale);
  49. this.height = (int)(element.ActualHeight * imageScale);
  50. this.totalWidth = this.width * parameterColumns;
  51. this.totalHeight = this.height * parameterLines;
  52. this.currentX = 0;
  53. this.currentY = 0;
  54. this.mainImage = new Image();
  55. this.drawingVisual = new DrawingVisual();
  56. this.bmp = new RenderTargetBitmap(this.totalWidth, this.totalHeight, DPI, DPI, PIXEL_FORMAT);
  57. }
  58. public void RenderMedia()
  59. {
  60. FormattedText text;
  61. DrawingContext drawingContext;
  62. RenderTargetBitmap bmpCapture;
  63. Rect rect;
  64. String textToDraw;
  65. bmpCapture = new RenderTargetBitmap(this.MediaElementWidth, this.MediaElementHeight, DPI, DPI, PIXEL_FORMAT);
  66. bmpCapture.Render(this.mediaElement);
  67. drawingContext = this.drawingVisual.RenderOpen();
  68. rect = new Rect(this.currentX, this.currentY, this.width, this.height);
  69. drawingContext.DrawImage(bmpCapture, rect);
  70. // Dessiner le temps sur l'image
  71. textToDraw = TimeUtils.FormatTimeSpan(this.mediaElement.Position);
  72. text = new FormattedText(textToDraw, CULTURE, FLOW_DIRECTION, TYPEFACE, TEXT_SIZE, COLOR);
  73. drawingContext.DrawText(text, new Point(this.currentX, this.currentY));
  74. drawingContext.Close();
  75. this.currentI++;
  76. if (this.currentI >= this.columns)
  77. {
  78. this.currentJ++;
  79. this.currentI = 0;
  80. }
  81. // Recommencer au début si le nombre de lignes a été dépasser.
  82. if (this.currentJ >= this.lines)
  83. {
  84. this.currentJ = 0;
  85. }
  86. this.currentX = this.currentI * this.width;
  87. this.currentY = this.currentJ * this.height;
  88. this.bmp.Render(this.drawingVisual);
  89. this.mainImage.Source = this.bmp;
  90. this.nbImages++;
  91. }
  92. public void launchAutoCapture(int imageCount)
  93. {
  94. TimeSpan[] frames = null;
  95. this.mediaElement.LoadedBehavior = MediaState.Play;
  96. Thread.Sleep(200);
  97. this.mediaElement.LoadedBehavior = MediaState.Pause;
  98. this.mediaElement.LoadedBehavior = MediaState.Play;
  99. Thread.Sleep(200);
  100. this.mediaElement.LoadedBehavior = MediaState.Pause;
  101. frames = TimeUtils.DivideTimeSpans(TimeUtils.AnalyseTotalDuration(this.mediaElement.Source), imageCount);
  102. foreach (TimeSpan frame in frames)
  103. {
  104. this.mediaElement.Position = frame;
  105. this.mediaElement.LoadedBehavior = MediaState.Play;
  106. Thread.Sleep(200);
  107. this.mediaElement.LoadedBehavior = MediaState.Pause;
  108. this.RenderMedia();
  109. }
  110. this.mediaElement.LoadedBehavior = MediaState.Stop;
  111. this.mediaElement.ScrubbingEnabled = false;
  112. }
  113. #region Properties
  114. private int MediaElementWidth
  115. {
  116. get
  117. {
  118. return (int)mediaElement.ActualWidth;
  119. }
  120. }
  121. private int MediaElementHeight
  122. {
  123. get
  124. {
  125. return (int)mediaElement.ActualHeight;
  126. }
  127. }
  128. public Boolean Closed
  129. {
  130. get
  131. {
  132. return this.closed;
  133. }
  134. }
  135. public Image MainImage
  136. {
  137. get
  138. {
  139. return this.mainImage;
  140. }
  141. }
  142. #endregion
  143. public void CloseImage(String desiredPath)
  144. {
  145. FileStream stream = null;
  146. JpegBitmapEncoder encoder = null;
  147. BitmapSource source;
  148. BitmapFrame bmpFrame;
  149. if (!this.Closed && this.nbImages > 0)
  150. {
  151. try
  152. {
  153. encoder = new JpegBitmapEncoder();
  154. source = (BitmapSource)this.mainImage.Source;
  155. bmpFrame = BitmapFrame.Create(source);
  156. encoder.Frames.Add(bmpFrame);
  157. stream = new FileStream(desiredPath, FileMode.Create);
  158. encoder.Save(stream);
  159. }
  160. finally
  161. {
  162. if (stream != null)
  163. {
  164. stream.Flush();
  165. stream.Close();
  166. }
  167. this.closed = true;
  168. }
  169. }
  170. }
  171. }
  172. }