-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
254 lines (230 loc) · 10.5 KB
/
MainWindow.xaml.cs
File metadata and controls
254 lines (230 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using Microsoft.Win32;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Metadata.Profiles.Iptc;
using SixLabors.ImageSharp.Metadata.Profiles.Xmp;
using System;
using System.IO;
using System.Text;
using System.Windows;
using MetadataExtractor;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Linq;
using MetadataExtractor.Formats.Exif;
using SixLabors.ImageSharp.Processing;
namespace MetadataEditor
{
public partial class MainWindow : Window
{
private string? _currentImagePath;
public MainWindow()
{
InitializeComponent();
}
private void OpenFileButton_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog
{
Title = "Выберите файл изображения",
Filter = "Файлы изображений|*.jpg;*.jpeg;*.png;*.gif;*.tiff;*.bmp|Все файлы|*.*"
};
if (openFileDialog.ShowDialog() == true)
{
_currentImagePath = openFileDialog.FileName;
DisplayMetadata(_currentImagePath);
DisplayImage(_currentImagePath);
}
}
private void PasteButton_Click(object sender, RoutedEventArgs e)
{
if (Clipboard.ContainsImage())
{
_currentImagePath = Path.Combine(Path.GetTempPath(), "pasted_image.png");
try
{
BitmapSource? bitmap = Clipboard.GetImage();
if (bitmap != null)
{
using (var fileStream = new FileStream(_currentImagePath, FileMode.Create))
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(fileStream);
}
DisplayMetadata(_currentImagePath);
DisplayImage(_currentImagePath);
}
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при вставке изображения: {ex.Message}", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
MessageBox.Show("Буфер обмена не содержит изображения.", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void DisplayMetadata(string filePath)
{
try
{
var directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(filePath);
var sb = new StringBuilder();
foreach (var directory in directories)
{
if (directory.Tags.Any())
{
sb.AppendLine($"--- {directory.Name} ---");
foreach (var tag in directory.Tags)
{
sb.AppendLine($"{tag.Name}: {tag.Description}");
}
sb.AppendLine();
}
}
MetadataTextBox.Text = sb.ToString();
}
catch (Exception ex)
{
MetadataTextBox.Text = $"Ошибка при чтении метаданных: {ex.Message}";
}
}
private void DisplayImage(string filePath)
{
try
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(filePath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
ImagePreview.Source = bitmap;
}
catch (Exception)
{
ImagePreview.Source = null;
}
}
private void CopyButton_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(MetadataTextBox.Text))
{
Clipboard.SetText(MetadataTextBox.Text);
MessageBox.Show("Метаданные скопированы в буфер обмена.", "Готово", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("Нет метаданных для копирования.", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(_currentImagePath))
{
MessageBox.Show("Сначала откройте изображение.", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var saveFileDialog = new SaveFileDialog
{
Title = "Сохранить файл без метаданных",
Filter = "Файлы изображений JPEG|*.jpg|Файлы изображений PNG|*.png",
FileName = Path.GetFileNameWithoutExtension(_currentImagePath) + "_nometa.jpg"
};
if (saveFileDialog.ShowDialog() == true)
{
try
{
using (var image = SixLabors.ImageSharp.Image.Load(_currentImagePath))
{
var directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(_currentImagePath);
var exifIfd0Directory = directories.OfType<ExifIfd0Directory>().FirstOrDefault();
if (exifIfd0Directory != null && exifIfd0Directory.TryGetUInt16(ExifDirectoryBase.TagOrientation, out var orientation))
{
switch (orientation)
{
case 2: image.Mutate(x => x.Flip(FlipMode.Horizontal)); break;
case 3: image.Mutate(x => x.Rotate(180)); break;
case 4: image.Mutate(x => x.Flip(FlipMode.Vertical)); break;
case 5: image.Mutate(x => x.Flip(FlipMode.Horizontal).Rotate(270)); break;
case 6: image.Mutate(x => x.Rotate(90)); break;
case 7: image.Mutate(x => x.Flip(FlipMode.Horizontal).Rotate(90)); break;
case 8: image.Mutate(x => x.Rotate(270)); break;
}
}
image.Metadata.ExifProfile = null;
image.Metadata.IptcProfile = null;
image.Metadata.XmpProfile = null;
image.Metadata.IccProfile = null;
string extension = Path.GetExtension(saveFileDialog.FileName).ToLower();
if (extension == ".png")
{
var pngEncoder = new PngEncoder
{
CompressionLevel = PngCompressionLevel.BestCompression,
SkipMetadata = true
};
image.Save(saveFileDialog.FileName, pngEncoder);
}
else
{
var jpegEncoder = new JpegEncoder
{
Quality = 85,
SkipMetadata = true
};
image.Save(saveFileDialog.FileName, jpegEncoder);
}
}
MessageBox.Show($"Файл успешно сохранён без метаданных: {saveFileDialog.FileName}", "Готово", MessageBoxButton.OK, MessageBoxImage.Information);
VerifyMetadataRemoved(saveFileDialog.FileName);
ClearFile();
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при сохранении файла: {ex.Message}", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void VerifyMetadataRemoved(string filePath)
{
try
{
var directories = MetadataExtractor.ImageMetadataReader.ReadMetadata(filePath);
var metadataCount = 0;
foreach (var directory in directories)
{
if (!directory.Name.Contains("JPEG") && !directory.Name.Contains("PNG") &&
!directory.Name.Contains("File") && !directory.Name.Contains("Image"))
{
metadataCount += directory.Tags.Count();
}
}
if (metadataCount > 0)
{
MessageBox.Show($"Внимание: в файле всё ещё обнаружено {metadataCount} тегов метаданных. " +
"Некоторые базовые метаданные могут сохраняться для корректного отображения изображения.",
"Информация", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Ошибка при проверке метаданных: {ex.Message}");
}
}
private void ClearFile()
{
MetadataTextBox.Text = string.Empty;
_currentImagePath = null;
ImagePreview.Source = null;
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
ClearFile();
}
}
}