-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveWindowsWallpaper2.cs
178 lines (161 loc) · 6.16 KB
/
SaveWindowsWallpaper2.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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
public class Program {
// 設定ファイルに関する変数
private static Settings settings = new Settings();
private static string SettingsPath;
// 検索対象フォルダや保存先フォルダ等の変数
private const string DIRECTORY_UID = "cw5n1h2txyewy";
private static string SearchDirectory, SaveDirectory, HorizontalDirectory, VerticalDirectory;
private static string[] ImagePaths;
public static void Main() {
PrepareImagePaths();
if (ImagePaths.Length > 0) {
loadSettings();
JudgeAndDeal();
saveSettings();
Console.WriteLine("Completed.");
} else {
Console.WriteLine("Files not found.");
}
//Console.WriteLine("Press any key to exit.");
//Console.ReadKey();
}
// 検索対象フォルダの指定や、保存先フォルダの指定等
private static void PrepareImagePaths() {
SearchDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Packages\\Microsoft.Windows.ContentDeliveryManager_" + DIRECTORY_UID + "\\LocalState\\Assets");
SaveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "SaveWindowsWallpaper");
HorizontalDirectory = Path.Combine(SaveDirectory, "Horizontal");
VerticalDirectory = Path.Combine(SaveDirectory, "Vertical");
ImagePaths = Directory.GetFiles(SearchDirectory);
SettingsPath = Path.Combine(SaveDirectory, "Settings.xml");
if (!Directory.Exists(SaveDirectory)) {
Directory.CreateDirectory(SaveDirectory);
}
if (!Directory.Exists(HorizontalDirectory)) {
Directory.CreateDirectory(HorizontalDirectory);
}
if (!Directory.Exists(VerticalDirectory)) {
Directory.CreateDirectory(VerticalDirectory);
}
}
// 画像の状態(内容等)を確認して、それに対応した処理を行う
private static void JudgeAndDeal() {
// 新規ファイルの全探索
foreach (string file in ImagePaths) {
// フォルダ名を除いて、ファイル名のみ取得する
string fileName = file.Substring(SearchDirectory.Length + 2);
string saveFilePath = "";
try {
using (Image image = Image.FromFile(file)) {
if (isHighDefinitionImage(image.Width, image.Height)) {
string hash = getHash(file);
if (settings.Hashes.Contains(hash)) {
Console.WriteLine("Deleted: Duplicate");
} else if (settings.BlackHashes.Contains(hash)) {
Console.WriteLine("Deleted: Blacklist");
} else {
if (isHorizontalImage(image.Width, image.Height)) {
saveFilePath = getOptimumPath(HorizontalDirectory, fileName);
Console.WriteLine("Moved: Horizontal");
} else if (isVerticalImage(image.Width, image.Height)) {
saveFilePath = getOptimumPath(VerticalDirectory, fileName);
Console.WriteLine("Moved: Vertical");
} else {
Console.WriteLine("Unknown error! Path: " + file);
continue;
}
settings.Hashes.Add(hash);
}
} else {
Console.WriteLine("Deleted: Too small");
}
}
} catch (OutOfMemoryException) {
Console.WriteLine("Deleted: Not image");
}
if (saveFilePath != "") {
File.Move(file, saveFilePath);
} else {
File.Delete(file);
}
}
}
private static bool isHighDefinitionImage(int width, int height) =>
(isHorizontalImage(width, height) || isVerticalImage(width, height));
private static bool isHorizontalImage(int width, int height) =>
(width >= 1920 && height >= 1080);
private static bool isVerticalImage(int width, int height) =>
(width >= 1080 && height >= 1920);
// 画像のハッシュ値を取得
private static string getHash(string file) {
Bitmap img = new Bitmap(file);
BitmapData bd = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat);
try {
int bsize = bd.Stride * img.Height;
byte[] bytes = new byte[bsize];
Marshal.Copy(bd.Scan0, bytes, 0, bsize);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(bytes);
return string.Join("", hash.Select(b => b.ToString()).ToArray());
} finally {
img.UnlockBits(bd);
img.Dispose();
}
}
// 保存に最適な名前を取得(重複していなければそのまま返却、重複していれば生成して返却)
private static string getOptimumPath(string directory, string name) {
string path = Path.Combine(directory, name + ".jpeg");
while (true) {
if (File.Exists(path)) {
path = Path.Combine(directory, getRandomCode());
if (path.Substring(path.Length-5) != ".jpeg") {
path += ".jpeg";
}
} else {
return path;
}
}
}
// 適当な文字列を返却
private static string getRandomCode() {
var result = "";
var codeChar = "0123456789abcdefghijklmnopqrstuvwxyz";
var rand = new Random();
for (var i = 0; i < 64; i++) {
var pos = rand.Next(codeChar.Length);
var code = codeChar[pos];
result += code;
}
return result;
}
private static void loadSettings() {
if (File.Exists(SettingsPath)) {
var serializer = new XmlSerializer(typeof(Settings));
var reader = new StreamReader(SettingsPath);
settings = (Settings)serializer.Deserialize(reader);
reader.Close();
} else {
saveSettings();
loadSettings();
}
}
private static void saveSettings() {
var serializer = new XmlSerializer(typeof(Settings));
var writer = new StreamWriter(SettingsPath, false, Encoding.UTF8);
serializer.Serialize(writer, settings);
writer.Close();
}
}
public class Settings {
public List<string> Hashes = new List<string>();
public List<string> BlackHashes = new List<string>();
}