-
Notifications
You must be signed in to change notification settings - Fork 2
/
Form1.cs
146 lines (127 loc) · 4.91 KB
/
Form1.cs
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
using System.IO;
namespace WeChatImageDecode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ImageTypeCounter counter;
if(FilesDropIn.Count > 0)
{
counter = DecodeFiles(FilesDropIn.ToArray<string>());
}
else
{
if (TextBox_SourceFolder.Text.Trim() == "" || !Directory.Exists(TextBox_SourceFolder.Text.Trim()))
{
return;
}
if (string.IsNullOrEmpty(TextBox_TargetFolder.Text.Trim()))
{
return;
}
if (!Directory.Exists(TextBox_TargetFolder.Text))
{
Directory.CreateDirectory(TextBox_TargetFolder.Text);
}
counter = DecodeFiles(Directory.GetFiles(TextBox_SourceFolder.Text),TextBox_TargetFolder.Text);
}
MessageBox.Show($"{(counter.JPG > 0 ? $"处理 JPG 格式 {counter.JPG} 个," : "")}{(counter.PNG > 0 ? $"PNG 格式 {counter.PNG} 个," : "" )}{(counter.GIF > 0 ? $"GIF 格式 {counter.GIF} 个," : "")}{(counter.BMP > 0 ? $"BMP 格式 {counter.BMP} 个," : "")}{(counter.UNKNOW > 0 ? $"未知格式 {counter.UNKNOW} 个" : "")}");
FilesDropIn.Clear();
}
private ImageTypeCounter DecodeFiles(string[] files, string targetFolder = "")
{
ImageTypeCounter counter = new ImageTypeCounter();
WeChatImageDecoder imgInfo = new WeChatImageDecoder();
foreach (var file in files)
{
if (Path.GetExtension(file).ToLower() != ".dat")
continue;
imgInfo.GetImageFormat(file, out int decodeValue, out WeChatImageDecoder.ImageFormat imgFormat);
if (imgFormat == WeChatImageDecoder.ImageFormat.PNG)
{
counter.PNG++;
}
else if (imgFormat == WeChatImageDecoder.ImageFormat.BMP)
{
counter.BMP++;
}
else if (imgFormat == WeChatImageDecoder.ImageFormat.GIF)
{
counter.GIF++;
}
else if (imgFormat == WeChatImageDecoder.ImageFormat.JPG)
{
counter.JPG++;
}
else
{
counter.UNKNOW++;
continue;
}
if(string.IsNullOrEmpty(targetFolder))
{
targetFolder = Path.GetDirectoryName(file);
}
var fileName = Path.Combine(targetFolder, Path.GetFileNameWithoutExtension(file) + "." + Enum.GetName(typeof(WeChatImageDecoder.ImageFormat), imgFormat).ToLower());
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(imgInfo.DecodeImage(file, decodeValue));
}
}
return counter;
}
struct ImageTypeCounter
{
public int JPG { get; set; }
public int PNG { get; set; }
public int BMP { get; set; }
public int GIF { get; set; }
public int UNKNOW { get; set; }
}
private void FolderBrowser(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
if(((Button)sender).Tag.ToString() == "source")
{
TextBox_SourceFolder.Text = dialog.SelectedPath;
}
else
{
TextBox_TargetFolder.Text = dialog.SelectedPath;
}
}
}
private void TextBox_SourceFolder_DragEnter(object sender, DragEventArgs e)
{
if (e.Data == null) return;
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
static List<string> FilesDropIn = new List<string>();
private void TextBox_SourceFolder_DragDrop(object sender, DragEventArgs e)
{
if(e.Data == null) return;
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (var file in files)
{
if(file.EndsWith(".dat"))
{
FilesDropIn.Add(file);
}
}
TextBox_SourceFolder.Text = $"共 {FilesDropIn.Count} 个文件";
}
}
}
}