-
Notifications
You must be signed in to change notification settings - Fork 2
/
WeChatImageDecoder.cs
126 lines (119 loc) · 4.47 KB
/
WeChatImageDecoder.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WeChatImageDecode
{
/// <summary>
///
/// </summary>
internal class WeChatImageDecoder
{
/*
* 文件头:
* jpg 文件:0xFFD8FF
* png 文件:0x89504E47
* gif 文件:0x47494638
* bmp 文件:0x424D
*/
/// <summary>
/// 图片文件头前两个字节的值
/// </summary>
public enum ImageHeaderValue : byte
{
JpegHeaderValue1 = 0xFF,
JpegHeaderValue2 = 0xD8,
PngHeaderValue1 = 0x89,
PngHeaderValue2 = 0x50,
GifHeaderValue1 = 0x47,
GifHeaderValue2 = 0x49,
BmpHeaderValue1 = 0x42,
BmpHeaderValue2 = 0x4D
}
/// <summary>
/// 图片格式
/// </summary>
public enum ImageFormat
{
GIF,
BMP,
PNG,
JPG,
UNKNOW
}
/// <summary>
/// 获取图片原来的格式
/// </summary>
/// <param name="fileName"></param>
/// <param name="decodeValue">将 dat 文件的第一个字节和对应格式头部的第一个字节进行异或操作的值,这个值就可以用来解码</param>
/// <param name="imageFormat">图片格式</param>
public void GetImageFormat(string fileName, out int decodeValue, out ImageFormat imageFormat)
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
//获取文件的前两位
byte[] data = new byte[2];
fs.Read(data, 0, 2);
//判断图片格式,将图片的前两个字节和对应的文件头进行异或操作
if ((data[0] ^ (byte)ImageHeaderValue.JpegHeaderValue1) == (data[1] ^ (byte)ImageHeaderValue.JpegHeaderValue2))
{
imageFormat = ImageFormat.JPG;
decodeValue = data[0] ^ (byte)ImageHeaderValue.JpegHeaderValue1;
}
else if ((data[0] ^ (byte)ImageHeaderValue.PngHeaderValue1) == (data[1] ^ (byte)ImageHeaderValue.PngHeaderValue2))
{
imageFormat = ImageFormat.PNG;
decodeValue = data[0] ^ (byte)ImageHeaderValue.PngHeaderValue1;
}
else if ((data[0] ^ (byte)ImageHeaderValue.GifHeaderValue1) == (data[1] ^ (byte)ImageHeaderValue.GifHeaderValue2))
{
imageFormat = ImageFormat.GIF;
decodeValue = data[0] ^ (byte)ImageHeaderValue.GifHeaderValue1;
}
else if ((data[0] ^ (byte)ImageHeaderValue.BmpHeaderValue1) == (data[1] ^ (byte)ImageHeaderValue.BmpHeaderValue2))
{
imageFormat = ImageFormat.BMP;
decodeValue = data[0] ^ (byte)ImageHeaderValue.BmpHeaderValue1;
}
else
{
imageFormat = ImageFormat.UNKNOW;
decodeValue = 0;
}
}
}
/// <summary>
/// 解码图片
/// </summary>
/// <param name="fileName">原始文件名</param>
/// <param name="encodeValue">dat 格式文件的第一个字节对相应格式异或后的值,用这个值对每个字节进行异或操作</param>
/// <returns>解码后的文件字节数组</returns>
public byte[] DecodeImage(string fileName, int encodeValue)
{
byte[] imgData ;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
imgData = new byte[fs.Length];
var readLength = 0;
var totalLen = imgData.Length;
while(totalLen > 0)
{
int n = fs.Read(imgData, readLength, totalLen);
if (n == 0)
{
break;
}
readLength += n;
totalLen -= n;
}
//对每个字节进行异或操作
for(int i = 0; i < imgData.Length; i++)
{
imgData[i] = (byte)(imgData[i] ^ encodeValue);
}
}
return imgData;
}
}
}