-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSvgSource.cs
More file actions
261 lines (244 loc) · 8.74 KB
/
SvgSource.cs
File metadata and controls
261 lines (244 loc) · 8.74 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using BytecodeApi.Wpf.Extensions;
using SharpVectors.Converters;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace BytecodeApi.Wpf.Cui;
/// <summary>
/// Class to convert SVG resources to WPF image sources.
/// </summary>
public static class SvgSource
{
private static readonly Dictionary<string, DrawingImage> ImageSourceCache = [];
private static readonly Dictionary<(string, Color), DrawingImage> ImageSourceColoredCache = [];
private static readonly Dictionary<(string, int), System.Drawing.Bitmap> BitmapCache = [];
private static readonly Dictionary<(string, int, Color), System.Drawing.Bitmap> BitmapColoredCache = [];
private static readonly Dictionary<(string, int), BitmapSource> BitmapSourceCache = [];
private static readonly Dictionary<(string, int, Color), BitmapSource> BitmapSourceColoredCache = [];
/// <summary>
/// Loads an SVG resource from the specified URI and converts it to a <see cref="DrawingImage" />.
/// </summary>
/// <param name="uri">The URI of the SVG resource.</param>
/// <returns>
/// A new <see cref="DrawingImage" /> if the resource was found;
/// otherwise, <see langword="null" />.
/// </returns>
public static DrawingImage? ImageSource(string uri)
{
Check.ArgumentNull(uri);
Check.ArgumentEx.StringNotEmpty(uri);
if (ImageSourceCache.TryGetValue(uri, out DrawingImage? existingImage))
{
return existingImage;
}
else if (GetSvgResource(uri) is DrawingImage image)
{
if (image.CanFreeze) image.Freeze();
ImageSourceCache[uri] = image;
return image;
}
else
{
return null;
}
}
/// <summary>
/// Loads an SVG resource from the specified URI, converts it to a <see cref="DrawingImage" />, and changes its color.
/// </summary>
/// <param name="uri">The URI of the SVG resource.</param>
/// <param name="color">A <see cref="Color" /> to apply to all SVG elements.</param>
/// <returns>
/// A new <see cref="DrawingImage" /> if the resource was found;
/// otherwise, <see langword="null" />.
/// </returns>
public static DrawingImage? ImageSource(string uri, Color color)
{
Check.ArgumentNull(uri);
Check.ArgumentEx.StringNotEmpty(uri);
if (ImageSourceColoredCache.TryGetValue((uri, color), out DrawingImage? existingImage))
{
return existingImage;
}
else if (GetSvgResource(uri) is DrawingImage image)
{
ChangeColor(image, color);
if (image.CanFreeze) image.Freeze();
ImageSourceColoredCache[(uri, color)] = image;
return image;
}
else
{
return null;
}
}
/// <summary>
/// Loads an SVG resource from the specified URI and converts it to a <see cref="System.Drawing.Bitmap" /> of the specified size.
/// </summary>
/// <param name="uri">The URI of the SVG resource.</param>
/// <param name="size">The width and height, in pixels, of the resulting <see cref="System.Drawing.Bitmap" />.</param>
/// <returns>
/// A new <see cref="System.Drawing.Bitmap" /> if the resource was found;
/// otherwise, <see langword="null" />.
/// </returns>
public static System.Drawing.Bitmap? Bitmap(string uri, int size)
{
Check.ArgumentNull(uri);
Check.ArgumentEx.StringNotEmpty(uri);
Check.ArgumentOutOfRangeEx.Greater0(size);
if (BitmapCache.TryGetValue((uri, size), out System.Drawing.Bitmap? existingImage))
{
return existingImage;
}
else if (BitmapSource(uri, size) is BitmapSource bitmapSource)
{
System.Drawing.Bitmap bitmap = bitmapSource.ToBitmap();
BitmapCache[(uri, size)] = bitmap;
return bitmap;
}
else
{
return null;
}
}
/// <summary>
/// Loads an SVG resource from the specified URI, converts it to a <see cref="System.Drawing.Bitmap" /> of the specified size, and changes its color.
/// </summary>
/// <param name="uri">The URI of the SVG resource.</param>
/// <param name="size">The width and height, in pixels, of the resulting <see cref="System.Drawing.Bitmap" />.</param>
/// <param name="color">A <see cref="Color" /> to apply to all SVG elements.</param>
/// <returns>
/// A new <see cref="System.Drawing.Bitmap" /> if the resource was found;
/// otherwise, <see langword="null" />.
/// </returns>
public static System.Drawing.Bitmap? Bitmap(string uri, int size, Color color)
{
Check.ArgumentNull(uri);
Check.ArgumentEx.StringNotEmpty(uri);
Check.ArgumentOutOfRangeEx.Greater0(size);
if (BitmapColoredCache.TryGetValue((uri, size, color), out System.Drawing.Bitmap? existingImage))
{
return existingImage;
}
else if (BitmapSource(uri, size, color) is BitmapSource bitmapSource)
{
System.Drawing.Bitmap bitmap = bitmapSource.ToBitmap();
BitmapColoredCache[(uri, size, color)] = bitmap;
return bitmap;
}
else
{
return null;
}
}
/// <summary>
/// Loads an SVG resource from the specified URI, converts it to a <see cref="System.Drawing.Bitmap" /> of the specified size, and returns an equivalent <see cref="System.Windows.Media.Imaging.BitmapSource" />.
/// </summary>
/// <param name="uri">The URI of the SVG resource.</param>
/// <param name="size">The width and height, in pixels, of the <see cref="System.Drawing.Bitmap" />.</param>
/// <returns>
/// A new <see cref="System.Windows.Media.Imaging.BitmapSource" /> if the resource was found;
/// otherwise, <see langword="null" />.
/// </returns>
public static BitmapSource? BitmapSource(string uri, int size)
{
Check.ArgumentNull(uri);
Check.ArgumentEx.StringNotEmpty(uri);
Check.ArgumentOutOfRangeEx.Greater0(size);
if (BitmapSourceCache.TryGetValue((uri, size), out BitmapSource? existingImage))
{
return existingImage;
}
else if (ImageSource(uri) is DrawingImage image)
{
BitmapSource bitmap = ConvertDrawingImageToBitmap(image, size);
BitmapSourceCache[(uri, size)] = bitmap;
return bitmap;
}
else
{
return null;
}
}
/// <summary>
/// Loads an SVG resource from the specified URI, converts it to a <see cref="System.Drawing.Bitmap" /> of the specified size, changes its color, and returns an equivalent <see cref="System.Windows.Media.Imaging.BitmapSource" />.
/// </summary>
/// <param name="uri">The URI of the SVG resource.</param>
/// <param name="size">The width and height, in pixels, of the <see cref="System.Drawing.Bitmap" />.</param>
/// <param name="color">A <see cref="Color" /> to apply to all SVG elements.</param>
/// <returns>
/// A new <see cref="System.Windows.Media.Imaging.BitmapSource" /> if the resource was found;
/// otherwise, <see langword="null" />.
/// </returns>
public static BitmapSource? BitmapSource(string uri, int size, Color color)
{
Check.ArgumentNull(uri);
Check.ArgumentEx.StringNotEmpty(uri);
Check.ArgumentOutOfRangeEx.Greater0(size);
if (BitmapSourceColoredCache.TryGetValue((uri, size, color), out BitmapSource? existingImage))
{
return existingImage;
}
else if (ImageSource(uri, color) is DrawingImage image)
{
BitmapSource bitmap = ConvertDrawingImageToBitmap(image, size);
BitmapSourceColoredCache[(uri, size, color)] = bitmap;
return bitmap;
}
else
{
return null;
}
}
private static byte[] GetResource(string uri)
{
StreamResourceInfo resourceInfo = Application
.GetResourceStream(new(uri))
?? throw new FileNotFoundException($"Resource '{uri}' not found.");
using MemoryStream memoryStream = new();
resourceInfo.Stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
private static DrawingImage GetSvgResource(string uri)
{
using FileSvgReader reader = new(new());
using MemoryStream memoryStream = new(GetResource(uri.StartsWith("pack://", StringComparison.OrdinalIgnoreCase) ? uri : $"{Packs.Application}{uri}"));
return new(reader.Read(memoryStream));
}
private static void ChangeColor(DrawingImage image, Color color)
{
SetColor(image.Drawing);
void SetColor(Drawing drawing)
{
if (drawing is DrawingGroup group)
{
foreach (Drawing child in group.Children)
{
SetColor(child);
}
}
else if (drawing is GeometryDrawing geometry)
{
if (geometry.Pen?.Brush is SolidColorBrush solidColorBrush1)
{
solidColorBrush1.Color = color;
}
if (geometry.Brush is SolidColorBrush solidColorBrush2)
{
solidColorBrush2.Color = color;
}
}
}
}
private static BitmapSource ConvertDrawingImageToBitmap(DrawingImage image, int size)
{
Image wpfImage = new() { Source = image };
wpfImage.Measure(new(size, size));
wpfImage.Arrange(new(0, 0, size, size));
RenderTargetBitmap renderTarget = new(size, size, 96, 96, PixelFormats.Pbgra32);
renderTarget.Render(wpfImage);
return renderTarget;
}
}