-
Notifications
You must be signed in to change notification settings - Fork 0
/
JpegUtils.cs
94 lines (80 loc) · 3.66 KB
/
JpegUtils.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
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace ShaCollisionFinder
{
public static class JpegUtils
{
// ReSharper disable once InconsistentNaming
private const int JPEG_SIZE_MAX = 65000;
private static ImageCodecInfo GetEncoderInfo(string mimeType) => ImageCodecInfo.GetImageEncoders().FirstOrDefault(t => t.MimeType == mimeType);
private static Image Compress(this Image image, int quality)
{
var encoderParameters = new EncoderParameters(1)
{
Param = { [0] = new EncoderParameter(Encoder.Quality, quality) }
};
var imageCodecInfo = GetEncoderInfo("image/jpeg");
var stream = new MemoryStream();
image.Save(stream, imageCodecInfo, encoderParameters);
return Image.FromStream(stream);
}
private static int GetCompressedImageSize(this Image image, int quality)
{
var encoderParameters = new EncoderParameters(1)
{
Param = { [0] = new EncoderParameter(Encoder.Quality, quality) }
};
var imageCodecInfo = GetEncoderInfo("image/jpeg");
var stream = new MemoryStream();
image.Save(stream, imageCodecInfo, encoderParameters);
return (int)stream.Length;
}
public static Image CompressToSize(this Image image, int maxSize)
{
int l = 1, r = 100;
while (r - l > 1)
{
var m = (r + l) >> 1;
if (image.GetCompressedImageSize(m) <= maxSize) l = m; else r = m;
}
return image.Compress(l);
}
public static Image ResizeImage(this Image image, int w, int h)
{
if (image.Width == w && image.Height == h) return image;
Image resizedImage = new Bitmap(w, h);
using (var g = Graphics.FromImage(resizedImage))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, 0, 0, w, h);
}
return resizedImage;
}
public static byte[] ReadImageAsJpegWithSize(string fileName, int w, int h)
{
var image = Image.FromFile(fileName, true);
image = image.ResizeImage(w, h);
image = image.CompressToSize(JPEG_SIZE_MAX); //image.CompressToSize(65528);
var stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
return stream.ToArray();
}
public static byte[] ReadImageAsJpegAndCompress(string fileName)
{
var image = Image.FromFile(fileName, true);
image = image.CompressToSize(JPEG_SIZE_MAX); //image.CompressToSize(65528);
var stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
return stream.ToArray();
}
public static byte[] MergeImages(string fileName1, string fileName2) =>
MergeJpegs(ReadImageAsJpegAndCompress(fileName1), ReadImageAsJpegAndCompress(fileName2));
public static byte[] MergeJpegs(byte[] content1, byte[] content2) => content1.Skip(2).Concat(content2.Skip(2)).ToArray();
public static byte[] MergeJpegs(string fileName1, string fileName2) => File.ReadAllBytes(fileName1).Skip(2).Concat(File.ReadAllBytes(fileName2).Skip(2)).ToArray();
}
}