-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAccountPicConverter.cs
104 lines (96 loc) · 3.23 KB
/
AccountPicConverter.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
using System;
using System.IO;
using System.Drawing;
namespace accountpicture_ms
{
class AccountPicConverter
{
static int Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please provide the .accountpicture-ms file path.");
return 1;
}
string filename = Path.GetFileNameWithoutExtension(args[0]);
Bitmap image96 = GetImage96(args[0]);
image96.Save(filename + "-96.bmp");
Bitmap image448 = GetImage448(args[0]);
image448.Save(filename + "-448.bmp");
Console.WriteLine("Extracted images successfully");
return 0;
}
public static Bitmap GetImage96(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
long position = Seek(fs, "JFIF", 0);
byte[] b = new byte[Convert.ToInt32(fs.Length)];
fs.Seek(position - 6, SeekOrigin.Begin);
fs.Read(b, 0, b.Length);
fs.Close();
fs.Dispose();
return GetBitmapImage(b);
}
public static Bitmap GetImage448(string path)
{
FileStream fs = new FileStream(path, FileMode.Open);
long position = Seek(fs, "JFIF", 100);
byte[] b = new byte[Convert.ToInt32(fs.Length)];
fs.Seek(position - 6, SeekOrigin.Begin);
fs.Read(b, 0, b.Length);
fs.Close();
fs.Dispose();
return GetBitmapImage(b);
}
public static Bitmap GetBitmapImage(byte[] imageBytes)
{
//var bitmapImage = new Bitmap();
//bitmapImage.BeginInit();
//bitmapImage.StreamSource = new MemoryStream(imageBytes);
//bitmapImage.EndInit();
//return bitmapImage;
var ms = new MemoryStream(imageBytes);
var bitmapImage = new Bitmap(ms);
return bitmapImage;
}
public static long Seek(System.IO.FileStream fs, string searchString, int startIndex)
{
char[] search = searchString.ToCharArray();
long result = -1, position = 0, stored = startIndex,
begin = fs.Position;
int c;
while ((c = fs.ReadByte()) != -1)
{
if ((char)c == search[position])
{
if (stored == -1 && position > 0 && (char)c == search[0])
{
stored = fs.Position;
}
if (position + 1 == search.Length)
{
result = fs.Position - search.Length;
fs.Position = result;
break;
}
position++;
}
else if (stored > -1)
{
fs.Position = stored + 1;
position = 1;
stored = -1;
}
else
{
position = 0;
}
}
if (result == -1)
{
fs.Position = begin;
}
return result;
}
}
}