fasdfsa commited on
Commit
05efe39
·
1 Parent(s): ebc2688

wpfeditor 打开图片

Browse files
src/WpfEditor/Constant.cs ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.IO;
3
+ using System.Reflection;
4
+
5
+ namespace WpfEditor
6
+ {
7
+ /// <summary>
8
+ /// 提供应用程序中使用的常量
9
+ /// </summary>
10
+ public static class Constant
11
+ {
12
+ /// <summary>
13
+ /// 可执行文件所在目录的路径
14
+ /// </summary>
15
+ public static readonly string ExecutablePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
16
+ }
17
+ }
src/WpfEditor/MD5.cs ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.IO;
3
+ using System.Security.Cryptography;
4
+ using System.Text;
5
+
6
+ namespace WpfEditor
7
+ {
8
+ /// <summary>
9
+ /// MD5哈希计算工具类
10
+ /// </summary>
11
+ public static class MD5
12
+ {
13
+ /// <summary>
14
+ /// 计算字符串的MD5哈希值
15
+ /// </summary>
16
+ /// <param name="input">输入字符串</param>
17
+ /// <returns>MD5哈希值的十六进制字符串表示</returns>
18
+ public static string ComputeHash(string input)
19
+ {
20
+ if (string.IsNullOrEmpty(input))
21
+ return string.Empty;
22
+
23
+ using (var md5 = System.Security.Cryptography.MD5.Create())
24
+ {
25
+ byte[] inputBytes = Encoding.UTF8.GetBytes(input);
26
+ byte[] hashBytes = md5.ComputeHash(inputBytes);
27
+
28
+ return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
29
+ }
30
+ }
31
+
32
+ /// <summary>
33
+ /// 计算文件的MD5哈希值
34
+ /// </summary>
35
+ /// <param name="filePath">文件路径</param>
36
+ /// <returns>MD5哈希值的十六进制字符串表示</returns>
37
+ /// <exception cref="FileNotFoundException">当文件不存在时抛出</exception>
38
+ public static string ComputeFileHash(string filePath)
39
+ {
40
+ if (!File.Exists(filePath))
41
+ throw new FileNotFoundException("无法找到文件", filePath);
42
+
43
+ using (var md5 = System.Security.Cryptography.MD5.Create())
44
+ using (var stream = File.OpenRead(filePath))
45
+ {
46
+ byte[] hashBytes = md5.ComputeHash(stream);
47
+ return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
48
+ }
49
+ }
50
+
51
+ /// <summary>
52
+ /// 验证字符串的MD5哈希值是否匹配
53
+ /// </summary>
54
+ /// <param name="input">输入字符串</param>
55
+ /// <param name="hash">要验证的MD5哈希值</param>
56
+ /// <returns>如果匹配返回true,否则返回false</returns>
57
+ public static bool VerifyHash(string input, string hash)
58
+ {
59
+ string computedHash = ComputeHash(input);
60
+ return string.Equals(computedHash, hash, StringComparison.OrdinalIgnoreCase);
61
+ }
62
+
63
+ /// <summary>
64
+ /// 验证文件的MD5哈希值是否匹配
65
+ /// </summary>
66
+ /// <param name="filePath">文件路径</param>
67
+ /// <param name="hash">要验证的MD5哈希值</param>
68
+ /// <returns>如果匹配返回true,否则返回false</returns>
69
+ public static bool VerifyFileHash(string filePath, string hash)
70
+ {
71
+ string computedHash = ComputeFileHash(filePath);
72
+ return string.Equals(computedHash, hash, StringComparison.OrdinalIgnoreCase);
73
+ }
74
+ }
75
+ }
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -53,6 +53,25 @@ namespace WpfEditor
53
  StatusText.Text = "新建文件";
54
  }
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  private void OpenFile_Click(object sender, RoutedEventArgs e)
57
  {
58
  if (_textChanged)
@@ -65,9 +84,22 @@ namespace WpfEditor
65
  }
66
 
67
  OpenFileDialog openFileDialog = new OpenFileDialog();
68
- openFileDialog.Filter = "文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
69
  if (openFileDialog.ShowDialog() == true)
70
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  try
72
  {
73
  Editor.Text = File.ReadAllText(openFileDialog.FileName);
 
53
  StatusText.Text = "新建文件";
54
  }
55
 
56
+ /// <summary>
57
+ /// 获取图像和JSON文件的路径
58
+ /// </summary>
59
+ /// <param name="m5">MD5哈希值</param>
60
+ /// <returns>包含图像和JSON文件路径的元组</returns>
61
+ public static (string ImagePath, string JsonPath) get_img_json_pth(string m5)
62
+ {
63
+ // 这是工作目录
64
+ string pthImg = Path.Combine(Constant.ExecutablePath, "data", "img", $"{m5}.txt"); // .parent_path().parent_path() 在C#中不需要
65
+ string pthJson = Path.Combine(Constant.ExecutablePath, "data", "json", $"{m5}.json"); // .parent_path().parent_path() 在C#中不需要
66
+
67
+ // C#中不需要lexically_normal(),Path.GetFullPath可以规范化路径
68
+ pthImg = Path.GetFullPath(pthImg);
69
+ pthJson = Path.GetFullPath(pthJson);
70
+
71
+ // 使用C# 7.0引入的命名元组返回结果
72
+ return (ImagePath: pthImg, JsonPath: pthJson);
73
+ }
74
+
75
  private void OpenFile_Click(object sender, RoutedEventArgs e)
76
  {
77
  if (_textChanged)
 
84
  }
85
 
86
  OpenFileDialog openFileDialog = new OpenFileDialog();
87
+ openFileDialog.Filter = "图片文件 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
88
  if (openFileDialog.ShowDialog() == true)
89
  {
90
+ string m5 = MD5.ComputeFileHash(openFileDialog.FileName);
91
+
92
+ var (pthImg, pthJson) = get_img_json_pth(m5);
93
+
94
+ if (!Path.Exists(pthJson) || !Path.Exists(pthImg))
95
+ {
96
+ MessageBox.Show("不存在 json 或 image", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
97
+ return;
98
+ }
99
+
100
+ string pth_img = Path.Combine(Constant.ExecutablePath, "data", "img", $"{m5}.txt"); // .parent_path().parent_path() 在C#中不需要
101
+ string pth_json = Path.Combine(Constant.ExecutablePath, "data", "json", $"{m5}.json");
102
+
103
  try
104
  {
105
  Editor.Text = File.ReadAllText(openFileDialog.FileName);
src/WpfEditor/TextEditorControl.cs CHANGED
@@ -1065,11 +1065,31 @@ namespace WpfEditor
1065
  public bool IsString { get; set; } = false;
1066
  public bool IsNumber { get; set; } = false;
1067
 
 
 
 
 
 
 
 
 
 
 
1068
  public TextCharacter(System.Text.Rune c)
1069
  {
1070
  Character = c;
1071
  }
1072
-
 
 
 
 
 
 
 
 
 
 
1073
  // 添加一个接受char的构造函数,方便兼容现有代码
1074
  public TextCharacter(char c)
1075
  {
 
1065
  public bool IsString { get; set; } = false;
1066
  public bool IsNumber { get; set; } = false;
1067
 
1068
+
1069
+ // 字符在图片上的坐标和宽高
1070
+ public int x { get; set; } = -1;
1071
+ public int y { get; set; } = -1;
1072
+ public int w { get; set; } = -1;
1073
+ public int h { get; set; } = -1;
1074
+
1075
+
1076
+ public string ImageMD5 { get; set; } = "";
1077
+
1078
  public TextCharacter(System.Text.Rune c)
1079
  {
1080
  Character = c;
1081
  }
1082
+
1083
+ public TextCharacter(System.Text.Rune c, int x, int y, int w, int h, string imageMd5)
1084
+ {
1085
+ Character = c;
1086
+ this.x = x;
1087
+ this.y = y;
1088
+ this.w = w;
1089
+ this.h = h;
1090
+ ImageMD5 = imageMd5;
1091
+ }
1092
+
1093
  // 添加一个接受char的构造函数,方便兼容现有代码
1094
  public TextCharacter(char c)
1095
  {
src/WpfEditor/WpfEditor.csproj CHANGED
@@ -8,4 +8,11 @@
8
  <UseWPF>true</UseWPF>
9
  </PropertyGroup>
10
 
 
 
 
 
 
 
 
11
  </Project>
 
8
  <UseWPF>true</UseWPF>
9
  </PropertyGroup>
10
 
11
+ <ItemGroup>
12
+ <None Include="..\..\data\**\*.*">
13
+ <Link>data\%(RecursiveDir)%(Filename)%(Extension)</Link>
14
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15
+ </None>
16
+ </ItemGroup>
17
+
18
  </Project>