-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddBlacklist.cs
92 lines (83 loc) · 3.03 KB
/
AddBlacklist.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
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;
public static void Main() {
string saveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "SaveWindowsWallpaper");
SettingsPath = Path.Combine(saveDirectory, "Settings.xml");
if (!Directory.Exists(saveDirectory)) {
Console.WriteLine("There is not settings file.");
return;
}
loadSettings();
Console.WriteLine("Enter a image path to add to blacklist.");
string imagePath = Console.ReadLine();
string hash = getHash(imagePath);
if (settings.BlackHashes.Contains(hash)) {
Console.WriteLine("Already added to blacklist.");
return;
} else if (settings.Hashes.Contains(hash)) {
settings.BlackHashes.Add(hash);
settings.Hashes.Remove(hash);
string verorhori = imagePath.Substring(saveDirectory.Length + 2);
string fileName = (verorhori[0] == 'H') ? verorhori.Substring(12) : verorhori.Substring(10);
string saveFile = Path.Combine(Path.Combine(saveDirectory, "Blacklist"), fileName);
File.Move(imagePath, saveFile);
Console.WriteLine("Add to blacklist was successful.");
} else {
Console.WriteLine("The image is not added to SaveWindowsWallpaper directory.");
return;
}
saveSettings();
Console.WriteLine("Press the any key to exit.");
Console.ReadKey();
}
// 画像のハッシュ値を取得
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 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>();
}