DFVideoControl.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace InABox.DynamicGrid
  15. {
  16. public class DFVideoControl : DynamicFormFieldControl<DFLayoutVideoField, DFLayoutVideoFieldProperties, byte[]>
  17. {
  18. Button PlayButton;
  19. Button PauseButton;
  20. Button DownloadButton;
  21. MediaElement Player;
  22. Grid grid;
  23. Point center;
  24. int rotation = 90;
  25. bool firstPlay = true;
  26. byte[]? Data;
  27. protected override FrameworkElement Create()
  28. {
  29. grid = new Grid();
  30. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  31. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  32. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  33. grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  34. PlayButton = new Button
  35. {
  36. Width = 25,
  37. Height = 25,
  38. Margin = new Thickness(5)
  39. };
  40. PlayButton.Content = new Image
  41. {
  42. Source = Wpf.Resources.play_button.AsBitmapImage(),
  43. };
  44. PlayButton.Click += PlayButton_Click;
  45. PauseButton = new Button
  46. {
  47. Width = 25,
  48. Height = 25,
  49. Margin = new Thickness(5)
  50. };
  51. PauseButton.Content = new Image
  52. {
  53. Source = Wpf.Resources.pause_button.AsBitmapImage(),
  54. };
  55. PauseButton.Click += PauseButton_Click;
  56. DownloadButton = new Button
  57. {
  58. Width = 25,
  59. Height = 25,
  60. Margin = new Thickness(5)
  61. };
  62. DownloadButton.Content = new Image
  63. {
  64. Source = Wpf.Resources.download.AsBitmapImage(),
  65. };
  66. DownloadButton.Click += DownloadButton_Click;
  67. StackPanel panel = new StackPanel();
  68. panel.Orientation = Orientation.Horizontal;
  69. panel.Children.Add(PlayButton);
  70. panel.Children.Add(PauseButton);
  71. panel.Children.Add(DownloadButton);
  72. panel.HorizontalAlignment = HorizontalAlignment.Center;
  73. panel.SetGridPosition(1, 0, 1, 2);
  74. Player = new MediaElement();
  75. Player.Stretch = System.Windows.Media.Stretch.Fill;
  76. Player.Visibility = Visibility.Visible;
  77. Player.VerticalAlignment = VerticalAlignment.Center;
  78. Player.HorizontalAlignment = HorizontalAlignment.Center;
  79. Player.SetGridPosition(0, 0, 1, 2);
  80. Player.LoadedBehavior = MediaState.Manual;
  81. grid.Children.Add(panel);
  82. grid.Children.Add(Player);
  83. return grid;
  84. }
  85. private void DownloadButton_Click(object sender, RoutedEventArgs e)
  86. {
  87. var dlg = new SaveFileDialog();
  88. dlg.AddExtension = true;
  89. dlg.DefaultExt = ".mp4";
  90. dlg.ShowDialog();
  91. if (!string.IsNullOrWhiteSpace(dlg.FileName))
  92. {
  93. System.IO.File.WriteAllBytes(dlg.FileName, GetValue());
  94. MessageBox.Show("Success - file saved to: "
  95. + System.Environment.NewLine
  96. + dlg.FileName);
  97. var dir = System.IO.Path.GetDirectoryName(dlg.FileName);
  98. ProcessStartInfo startInfo = new ProcessStartInfo
  99. {
  100. Arguments = dir,
  101. FileName = "explorer.exe"
  102. };
  103. Process.Start(startInfo);
  104. }
  105. }
  106. private void PauseButton_Click(object sender, RoutedEventArgs e)
  107. {
  108. Player.Pause();
  109. }
  110. private void PlayButton_Click(object sender, RoutedEventArgs e)
  111. {
  112. var path = System.IO.Path.GetTempFileName();
  113. path = path.Substring(0, path.Length - 4) + ".mp4";
  114. System.IO.File.WriteAllBytes(path, GetValue());
  115. Player.Source = new Uri(path);
  116. Player.Play();
  117. }
  118. public override byte[] GetValue()
  119. {
  120. return Data ?? Array.Empty<byte>();
  121. }
  122. public override void SetValue(byte[]? value)
  123. {
  124. Data = value;
  125. }
  126. protected override bool IsEmpty() => Data is null || Data.Length == 0;
  127. }
  128. }