MainWindow.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using WatchDog.WPF;
  18. namespace YoloV5ImageLableWpfApp
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. private static FileSelectedVM _fileSelectedVM = new FileSelectedVM();
  26. public FileSelected fileSelected = new FileSelected(_fileSelectedVM);
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. this.Closed += MainWindow_Closed;
  31. //this.image.Source = new ImageSource();
  32. image.Source = new BitmapImage(new Uri(@"C:\Users\77411\Desktop\images2\进线区_2FA南侧_球机-18-17-57.bmp", UriKind.Absolute));
  33. this.canvas.MouseEnter += Canvas_MouseEnter;
  34. this.canvas.MouseMove += Canvas_MouseMove;
  35. this.canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
  36. this.canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp;
  37. this.KeyDown += MainWindow_KeyDown;
  38. }
  39. private void MainWindow_KeyDown(object sender, KeyEventArgs e)
  40. {
  41. if (Keyboard.IsKeyDown(Key.LeftCtrl) &&Keyboard.IsKeyDown(Key.Z) )
  42. {
  43. //只要当下同时按下的键中包含LeftCtrl、H和C,就会进入
  44. canvas.Children.Remove(rct);
  45. isStarted = false; isComplete = false;
  46. }
  47. }
  48. private void Canvas_MouseEnter(object sender, MouseEventArgs e)
  49. {
  50. }
  51. private void Canvas_MouseMove(object sender, MouseEventArgs e)
  52. {
  53. if (isStarted && !isComplete)
  54. {
  55. endPoint = e.GetPosition(canvas);
  56. if (endPoint.X - startPoint.X>0)
  57. {
  58. rct.Width = endPoint.X - startPoint.X;
  59. }
  60. else
  61. {
  62. rct.Width =Math.Abs( endPoint.X - startPoint.X);
  63. Canvas.SetLeft(rct, endPoint.X);
  64. }
  65. if (endPoint.Y - startPoint.Y>0)
  66. {
  67. rct.Height = Math.Abs(endPoint.Y - startPoint.Y);
  68. }
  69. else
  70. {
  71. rct.Height = Math.Abs(endPoint.Y - startPoint.Y);
  72. Canvas.SetTop(rct, endPoint.Y);
  73. }
  74. }
  75. }
  76. Rectangle rct ;
  77. Point startPoint;
  78. Point endPoint;
  79. bool isComplete = false;
  80. bool isStarted = false;
  81. private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  82. {
  83. isComplete = true;
  84. isStarted = false;
  85. }
  86. private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  87. {
  88. isStarted = true;
  89. startPoint = e.GetPosition(canvas);
  90. rct = new Rectangle();
  91. Canvas.SetLeft(rct, startPoint.X);
  92. Canvas.SetTop(rct, startPoint.Y);
  93. rct.StrokeThickness = 1;
  94. rct.Stroke = new SolidColorBrush(Color.FromRgb(255,255,255));
  95. this.canvas.Children.Add(rct);
  96. }
  97. private void MainWindow_Closed(object? sender, EventArgs e)
  98. {
  99. System.Environment.Exit(0);
  100. }
  101. private void Button_Click(object sender, RoutedEventArgs e)
  102. {
  103. fileSelected.ShowDialog();
  104. this.filePath.Text = _fileSelectedVM.CurrentDir;
  105. var dirInfos = Directory.GetFiles(_fileSelectedVM.CurrentDir);
  106. this.imageList.Items.Clear();
  107. foreach (var item in dirInfos)
  108. {
  109. Regex reg = new Regex("(bmp|jpg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|jpeg)");//这是搜索匹配0-9的数字
  110. if (reg.IsMatch(item))
  111. {
  112. this.imageList.Items.Add(new MyFile(System.IO.Path.GetFileName(item), item));
  113. }
  114. }
  115. }
  116. private void Button_Click_1(object sender, RoutedEventArgs e)
  117. {
  118. isComplete = false;
  119. try
  120. {
  121. var w = rct.ActualWidth / canvas.ActualWidth;
  122. var h = rct.ActualWidth / canvas.ActualHeight;
  123. var x = Canvas.GetLeft(rct) / canvas.ActualWidth + (w/ 2);
  124. var y = Canvas.GetTop(rct) / canvas.ActualHeight +(h/2);
  125. using (StreamWriter sw = new StreamWriter(_fileSelectedVM.CurrentDir+ "\\"+System.IO.Path.GetFileNameWithoutExtension( this.filePath.Text)+".txt"))
  126. {
  127. sw.WriteLine($"{this.lableName.Text} {x} {y} {w} {h}");
  128. }
  129. }
  130. catch (Exception ex)
  131. {
  132. MessageBox.Show(ex.Message);
  133. }
  134. }
  135. private void imageList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  136. {
  137. try
  138. {
  139. canvas.Children.Remove(rct);
  140. ListBox listBox = sender as ListBox;
  141. MyFile myFile = listBox.SelectedItem as MyFile;
  142. image.Source = new BitmapImage(new Uri(myFile.Path, UriKind.Absolute));
  143. filePath.Text = myFile.Path;
  144. }
  145. catch (Exception ex)
  146. {
  147. MessageBox.Show(ex.Message);
  148. }
  149. }
  150. }
  151. }