fasdfsa commited on
Commit
1779b47
·
1 Parent(s): ed50718

打开 pdf 文件加载所有同目录 .jpg

Browse files
Files changed (1) hide show
  1. src/WpfEditor/MainWindow.xaml.cs +63 -8
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -14,6 +14,7 @@ using static System.Net.Mime.MediaTypeNames;
14
  using System.Windows.Shapes;
15
  using System.Collections.Generic;
16
  using System.Windows.Controls.Primitives;
 
17
 
18
  using Window = System.Windows.Window;
19
  using Point = System.Windows.Point;
@@ -21,6 +22,19 @@ using Path = System.IO.Path;
21
 
22
  namespace WpfEditor
23
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  public enum EditorMode
25
  {
26
  ImageOCR,
@@ -171,10 +185,29 @@ namespace WpfEditor
171
  // 图片列表选择变化事件
172
  private void ImageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
173
  {
174
- //if (ImageListBox.SelectedItem is ImageFileInfo selectedImage)
175
- //{
176
- // LoadImageFile(selectedImage.FullPath);
177
- //}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  }
179
 
180
 
@@ -230,7 +263,7 @@ namespace WpfEditor
230
 
231
  OpenFileDialog openFileDialog = new OpenFileDialog();
232
  //openFileDialog.Filter = "图片文件 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
233
- openFileDialog.Filter = "图片文件 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|PDF文件 (*.pdf)|(*.pdf)"; // |文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*
234
 
235
  if (openFileDialog.ShowDialog() == true)
236
  {
@@ -262,9 +295,31 @@ namespace WpfEditor
262
  */
263
 
264
  string pth_pdf = openFileDialog.FileName;
265
-
266
-
267
- int a = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  }
269
  else
270
  {
 
14
  using System.Windows.Shapes;
15
  using System.Collections.Generic;
16
  using System.Windows.Controls.Primitives;
17
+ using System.Linq; // 添加这个引用
18
 
19
  using Window = System.Windows.Window;
20
  using Point = System.Windows.Point;
 
22
 
23
  namespace WpfEditor
24
  {
25
+ // 添加图片文件信息类
26
+ public class ImageFileInfo
27
+ {
28
+ public string Name { get; set; }
29
+ public string FullPath { get; set; }
30
+
31
+ public ImageFileInfo(string fullPath)
32
+ {
33
+ FullPath = fullPath;
34
+ Name = Path.GetFileName(fullPath);
35
+ }
36
+ }
37
+
38
  public enum EditorMode
39
  {
40
  ImageOCR,
 
185
  // 图片列表选择变化事件
186
  private void ImageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
187
  {
188
+ if (ImageListBox.SelectedItem is ImageFileInfo selectedImage)
189
+ {
190
+ // 弹窗显示图片路径
191
+ MessageBox.Show($"图片路径:\n{selectedImage.FullPath}",
192
+ "图片信息",
193
+ MessageBoxButton.OK,
194
+ MessageBoxImage.Information);
195
+
196
+ // 可选:在右侧面板显示选中的图片
197
+ try
198
+ {
199
+ BitmapImage bitmap = new BitmapImage();
200
+ bitmap.BeginInit();
201
+ bitmap.UriSource = new Uri(selectedImage.FullPath);
202
+ bitmap.EndInit();
203
+
204
+ ShowImageInPanel(bitmap);
205
+ }
206
+ catch (Exception ex)
207
+ {
208
+ StatusText.Text = $"加载图片失败: {ex.Message}";
209
+ }
210
+ }
211
  }
212
 
213
 
 
263
 
264
  OpenFileDialog openFileDialog = new OpenFileDialog();
265
  //openFileDialog.Filter = "图片文件 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
266
+ openFileDialog.Filter = "图片文件 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|PDF文件 (*.pdf)|(*.pdf)|所有文件 (*.*)|*.*";// |文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*
267
 
268
  if (openFileDialog.ShowDialog() == true)
269
  {
 
295
  */
296
 
297
  string pth_pdf = openFileDialog.FileName;
298
+
299
+ // 获取PDF文件所在目录
300
+ string pdfDirectory = Path.GetDirectoryName(pth_pdf);
301
+
302
+ // 查找同目录下的所有JPG图片文件
303
+ var jpgFiles = Directory.GetFiles(pdfDirectory, "*.jpg", SearchOption.TopDirectoryOnly)
304
+ .Concat(Directory.GetFiles(pdfDirectory, "*.jpeg", SearchOption.TopDirectoryOnly))
305
+ .OrderBy(f => f)
306
+ .ToList();
307
+
308
+ // 清空现有的图片列表
309
+ ImageListBox.Items.Clear();
310
+
311
+ // 将JPG图片添加到列表中
312
+ foreach (string jpgFile in jpgFiles)
313
+ {
314
+ ImageListBox.Items.Add(new ImageFileInfo(jpgFile));
315
+ }
316
+
317
+ // 更新状态栏
318
+ StatusText.Text = $"已加载PDF文件及同目录下的 {jpgFiles.Count} 个JPG图片";
319
+
320
+ // 设置当前文件路径
321
+ _currentFilePath = pth_pdf;
322
+ UpdateTitle();
323
  }
324
  else
325
  {