DFVideoControl.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using Microsoft.Win32;
  4. using Syncfusion.XPS;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Media;
  14. using FastReport.DevComponents.DotNetBar;
  15. using Microsoft.Xaml.Behaviors.Core;
  16. namespace InABox.DynamicGrid
  17. {
  18. public class DFVideoControl : DynamicFormFieldControl<DFLayoutVideoField, DFLayoutVideoFieldProperties, byte[], DFLayoutEmbeddedMediaValue>
  19. {
  20. private DFLayoutEmbeddedMediaValue _value = null!;
  21. private Button? _playButton;
  22. private Button? _pauseButton;
  23. private Button? _downloadButton;
  24. private MediaElement _player = null!;
  25. private Border? _border;
  26. private Image? _thumbnail;
  27. private Grid? _contentGrid;
  28. private Grid? _grid;
  29. /*Point center;
  30. int rotation = 90;
  31. bool firstPlay = true;*/
  32. private string _tempfilename = null!;
  33. byte[]? Data;
  34. protected override FrameworkElement Create()
  35. {
  36. _tempfilename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(),".mp4");
  37. _value = new DFLayoutEmbeddedMediaValue();
  38. _grid = new Grid();
  39. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  40. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  41. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  42. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
  43. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  44. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  45. _border = new Border()
  46. {
  47. BorderBrush = new System.Windows.Media.SolidColorBrush(Colors.Gray),
  48. BorderThickness = new Thickness(0.75),
  49. Background = new System.Windows.Media.SolidColorBrush(Colors.White),
  50. Padding = new Thickness(4),
  51. Margin = new Thickness(0)
  52. };
  53. _border.SetGridPosition(0,0,5,1);
  54. _grid.Children.Add(_border);
  55. _contentGrid = new Grid();
  56. _contentGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
  57. _contentGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
  58. _border.Child = _contentGrid;
  59. _thumbnail = new Image
  60. {
  61. StretchDirection = StretchDirection.DownOnly,
  62. Source = EmbeddedImageUtilities.CreateEmptyImage()
  63. };
  64. _thumbnail.Visibility = Visibility.Visible;
  65. _thumbnail.SetGridPosition(0, 0, 4, 1);
  66. _contentGrid.Children.Add(_thumbnail);
  67. _player = new MediaElement
  68. {
  69. Stretch = System.Windows.Media.Stretch.Fill,
  70. Visibility = Visibility.Visible,
  71. VerticalAlignment = VerticalAlignment.Center,
  72. HorizontalAlignment = HorizontalAlignment.Center,
  73. LoadedBehavior = MediaState.Manual,
  74. Source = new Uri(_tempfilename)
  75. };
  76. _player.Visibility = Visibility.Collapsed;
  77. _player.MediaOpened += (sender, args) =>
  78. {
  79. _player.Visibility = Visibility.Visible;
  80. _thumbnail.Visibility = Visibility.Collapsed;
  81. };
  82. _player.MediaEnded += (sender, args) =>
  83. {
  84. _player.Visibility = Visibility.Collapsed;
  85. _thumbnail.Visibility = Visibility.Visible;
  86. _player.Close();
  87. };
  88. _player.SetGridPosition(0, 0, 4, 1);
  89. _contentGrid.Children.Add(_player);
  90. _playButton = new Button
  91. {
  92. Margin = new Thickness(5,0,0,0),
  93. Content = new Image { Source = Wpf.Resources.play_button.AsBitmapImage(24,24) },
  94. Command = new ActionCommand(PlayButton_Click),
  95. Width = 30,
  96. Height = 30
  97. };
  98. _playButton.SetGridPosition(0, 1);
  99. _grid.Children.Add(_playButton);
  100. _pauseButton = new Button
  101. {
  102. Margin = new Thickness(5,5,0,0),
  103. Content = new Image { Source = Wpf.Resources.pause_button.AsBitmapImage(32,32) },
  104. Command = new ActionCommand(PauseButton_Click),
  105. Width = 30,
  106. Height = 30
  107. };
  108. _pauseButton.SetGridPosition(1, 1);
  109. _grid.Children.Add(_pauseButton);
  110. _downloadButton = new Button
  111. {
  112. Margin = new Thickness(5,5,0,0),
  113. Content = new Image { Source = Wpf.Resources.download.AsBitmapImage(32,32) },
  114. Command = new ActionCommand(DownloadButton_Click),
  115. Width = 30,
  116. Height = 30
  117. };
  118. _downloadButton.SetGridPosition(3, 1);
  119. _grid.Children.Add(_downloadButton);
  120. return _grid;
  121. }
  122. private void DownloadButton_Click()
  123. {
  124. var dlg = new SaveFileDialog();
  125. dlg.AddExtension = true;
  126. dlg.DefaultExt = ".mp4";
  127. dlg.ShowDialog();
  128. if (!string.IsNullOrWhiteSpace(dlg.FileName))
  129. {
  130. System.IO.File.WriteAllBytes(dlg.FileName, GetValue());
  131. MessageBox.Show("Success - file saved to: "
  132. + System.Environment.NewLine
  133. + dlg.FileName);
  134. var dir = System.IO.Path.GetDirectoryName(dlg.FileName);
  135. ProcessStartInfo startInfo = new ProcessStartInfo
  136. {
  137. Arguments = dir,
  138. FileName = "explorer.exe"
  139. };
  140. Process.Start(startInfo);
  141. }
  142. }
  143. private void PauseButton_Click()
  144. {
  145. _player.Pause();
  146. }
  147. private void PlayButton_Click()
  148. {
  149. if (System.IO.File.Exists(_tempfilename))
  150. {
  151. _player.Play();
  152. }
  153. else
  154. {
  155. DigitalFormDocumentFactory.LoadDocument(_value.ID, (data) =>
  156. {
  157. Dispatcher.BeginInvoke(() =>
  158. {
  159. System.IO.File.WriteAllBytes(_tempfilename, data);
  160. _player.Position = new TimeSpan(0);
  161. _player.Play();
  162. });
  163. });
  164. }
  165. }
  166. public override DFLayoutEmbeddedMediaValue GetSerializedValue()
  167. {
  168. if ((_value.Data?.Any() == true) && (_value.ID == Guid.Empty))
  169. _value.ID = DigitalFormDocumentFactory.SaveDocument(_value.Data);
  170. return _value;
  171. }
  172. public override void SetSerializedValue(DFLayoutEmbeddedMediaValue value)
  173. {
  174. _value = value;
  175. EmbeddedImageUtilities.LoadImageFromData(_thumbnail, _value.Thumbnail);
  176. }
  177. public override byte[] GetValue()
  178. {
  179. return Data ?? Array.Empty<byte>();
  180. }
  181. public override void SetValue(byte[]? value)
  182. {
  183. Data = value;
  184. }
  185. protected override bool IsEmpty() => Data is null || Data.Length == 0;
  186. }
  187. }