-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathImages.cs
153 lines (127 loc) · 5.42 KB
/
Images.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
/* Copyright (C) 2021 Dylan Cheng (https://github.com/newlooper)
This file is part of VirtualSpace.
VirtualSpace is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
VirtualSpace is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with VirtualSpace. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using VirtualSpace.AppLogs;
namespace VirtualSpace.Helpers
{
internal static class PathInfo
{
public const string WIDTH_HEIGHT_SPLITTER = "x";
}
public static class Images
{
public static Bitmap GetScaledBitmap( int width, int height, string path, ref Wallpaper wp, string cachePath, long quality )
{
var cached = Wallpaper.CachedWallPaper( path, cachePath, width, height );
if ( cached != null ) return cached;
using ( var src = new Bitmap( path ) )
{
var dest = new Bitmap( width, height, PixelFormat.Format32bppPArgb );
using ( var gr = Graphics.FromImage( dest ) )
{
gr.DrawImage( src, new Rectangle( Point.Empty, dest.Size ) );
}
var md5Path = Wallpaper.Md5Hash( path );
var file = Path.Combine( cachePath, md5Path.Str0, md5Path.Str1, width + PathInfo.WIDTH_HEIGHT_SPLITTER + height,
md5Path.FullString + "_" + Environment.CurrentManagedThreadId );
// dest.Save( file, ImageFormat.Jpeg );
var jpgEncoder = GetEncoder( ImageFormat.Jpeg );
var encoder = System.Drawing.Imaging.Encoder.Quality;
var encoderParameters = new EncoderParameters( 1 );
var encoderParameter = new EncoderParameter( encoder, quality );
encoderParameters.Param[0] = encoderParameter;
dest.Save( file, jpgEncoder, encoderParameters );
wp.Fullpath = file;
return dest;
}
}
private static ImageCodecInfo GetEncoder( ImageFormat format )
{
var codecs = ImageCodecInfo.GetImageEncoders();
foreach ( var codec in codecs )
{
if ( codec.FormatID == format.Guid )
{
return codec;
}
}
return null;
}
public static Icon BytesToIcon( object bytes )
{
using var ms = new MemoryStream( (byte[])bytes );
return new Icon( ms );
}
public static Bitmap BytesToBitmap( object bytes )
{
using var ms = new MemoryStream( (byte[])bytes );
return new Bitmap( ms );
}
}
public class Wallpaper
{
public Bitmap? Image { get; set; }
public Color Color { get; set; }
public string? Fullpath { get; set; }
public static Bitmap? CachedWallPaper( string path, string cachePath, int width, int height )
{
var cached = CachedWallPaperInfo( path, cachePath, width, height );
return cached.Exists ? new Bitmap( cached.Path ) : null;
}
public static (bool Exists, string Path) CachedWallPaperInfo( string path, string cachePath, int width, int height )
{
var md5Path = Md5Hash( path );
var targetPath = Path.Combine( cachePath, md5Path.Str0, md5Path.Str1, width + PathInfo.WIDTH_HEIGHT_SPLITTER + height );
Directory.CreateDirectory( targetPath );
var filepath = Path.Combine( targetPath, md5Path.FullString );
return new ValueTuple<bool, string>( File.Exists( filepath ), filepath );
}
public static (string FullString, string Str0, string Str1) Md5Hash( string input )
{
var md5 = MD5.Create();
var inputBytes = Encoding.ASCII.GetBytes( input );
var hashBytes = md5.ComputeHash( inputBytes );
var sb = new StringBuilder();
foreach ( var b in hashBytes )
{
sb.Append( b.ToString( "x2" ) );
}
var md5Str = sb.ToString();
return new ValueTuple<string, string, string>(
md5Str,
md5Str.Substring( 0, 1 ),
md5Str.Substring( 1, 1 )
);
}
public void Release()
{
Image?.Dispose();
Image = null;
if ( string.IsNullOrEmpty( Fullpath ) ) return;
try
{
var file = Regex.Replace( Fullpath, @"(.*?)_\d+$", "$1" );
if ( !File.Exists( file ) )
File.Move( Fullpath, file );
}
catch ( Exception ex )
{
Logger.Warning( "Delete cache file: " + ex.Message );
}
finally
{
File.Delete( Fullpath );
}
}
}
}