-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
167 lines (140 loc) · 6.8 KB
/
Utils.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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Vanity
{
internal static class Utils
{
// -----------------------------------------------------------------------------------------------------------------
// http://stackoverflow.com/questions/12416249/hashing-a-string-with-sha256
public static string ComputeSHA256ForString( string stringInput )
{
SHA256Managed crypt = new SHA256Managed();
string hash = String.Empty;
byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(stringInput), 0, Encoding.UTF8.GetByteCount(stringInput));
foreach ( byte bit in crypto )
{
hash += bit.ToString( "x2" );
}
return hash;
}
// -----------------------------------------------------------------------------------------------------------------
public static string ComputeSHA256ForFile( string file )
{
SHA256Managed crypt = new SHA256Managed();
using ( FileStream stream = File.OpenRead( file ) )
{
byte[] checksum = crypt.ComputeHash(stream);
return BitConverter.ToString( checksum ).Replace( "-", String.Empty );
}
}
// -----------------------------------------------------------------------------------------------------------------
public static void LaunchJpegOptim64( string file )
{
StringBuilder jpegArgs = new StringBuilder();
jpegArgs.Append( " --strip-com --strip-exif -p -q " );
jpegArgs.Append( "\"" );
jpegArgs.Append( file );
jpegArgs.Append( "\"" );
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();
startInfo.FileName = "jpegoptim64.exe";
startInfo.Arguments = jpegArgs.ToString();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
p.StartInfo = startInfo;
p.OutputDataReceived += ( sender, args ) => { if ( !string.IsNullOrEmpty( args.Data ) ) Console.WriteLine( "#> {0}", args.Data ); };
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
// -----------------------------------------------------------------------------------------------------------------
public static Image GenerateThumbnail( Image source, Int32 thumbWidth )
{
// figure out thumbnail dimensions given a fixed width
float imageAspect = (float)source.Height / (float)source.Width;
int thumbHeight = (int)((float)thumbWidth * imageAspect);
// resize with the best that the framework can give us, quality wise
var thumbnailImage = new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format24bppRgb);
using ( var gr = Graphics.FromImage( thumbnailImage ) )
{
gr.Clear( Color.Black );
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage( source, new Rectangle( -1, -1, thumbWidth + 2, thumbHeight + 2 ) );
}
return thumbnailImage;
}
// -----------------------------------------------------------------------------------------------------------------
public static Image GenerateAlbumThumbnail( Image source, Int32 thumbWidth, Int32 thumbHeight )
{
Image resizedToWidth = GenerateThumbnail(source, thumbWidth);
var sourceCrop = new Rectangle(0, 0, resizedToWidth.Width, resizedToWidth.Height);
if ( resizedToWidth.Height > thumbHeight )
{
Int32 diff = (resizedToWidth.Height - thumbHeight);
sourceCrop.Y += (diff / 2);
sourceCrop.Height -= diff;
}
// resize with the best that the framework can give us, quality wise
var thumbnailImage = new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format24bppRgb);
using ( var gr = Graphics.FromImage( thumbnailImage ) )
{
var fullRect = new Rectangle(0, 0, thumbWidth, thumbHeight);
gr.Clear( Color.Black );
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage( resizedToWidth, fullRect, sourceCrop, GraphicsUnit.Pixel );
}
return thumbnailImage;
}
// -----------------------------------------------------------------------------------------------------------------
public static ImageCodecInfo GetEncoder( ImageFormat format )
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach ( ImageCodecInfo codec in codecs )
{
if ( codec.FormatID == format.Guid )
{
return codec;
}
}
return null;
}
// -----------------------------------------------------------------------------------------------------------------
public static string RenderEXIFTag( object tagValue )
{
// Arrays don't render well without assistance.
if ( tagValue is Array array )
{
// Hex rendering for really big byte arrays (ugly otherwise)
if ( array.Length > 20 &&
array.GetType().GetElementType() == typeof( byte ) )
{
return "0x" + string.Join( "", array.Cast<byte>().Select( x => x.ToString( "X2" ) ).ToArray() );
}
return string.Join( ", ", array.Cast<object>().Select( x => x.ToString() ).ToArray() );
}
return tagValue.ToString();
}
// -----------------------------------------------------------------------------------------------------------------
public static string PrettifyName( string name )
{
string result = name.Replace('-', ' ').Replace('_', ' ');
TextInfo textInfo = new CultureInfo("en-GB", false).TextInfo;
return textInfo.ToTitleCase( result );
}
}
}