9
9
using System . Windows . Media . Imaging ;
10
10
using Flow . Launcher . Infrastructure . Logger ;
11
11
using Flow . Launcher . Infrastructure . Storage ;
12
+ using SharpVectors . Converters ;
13
+ using SharpVectors . Renderers . Wpf ;
12
14
13
15
namespace Flow . Launcher . Infrastructure . Image
14
16
{
@@ -25,8 +27,10 @@ public static class ImageLoader
25
27
public static ImageSource LoadingImage { get ; } = new BitmapImage ( new Uri ( Constant . LoadingImgIcon ) ) ;
26
28
public const int SmallIconSize = 64 ;
27
29
public const int FullIconSize = 256 ;
30
+ public const int FullImageSize = 320 ;
28
31
29
32
private static readonly string [ ] ImageExtensions = { ".png" , ".jpg" , ".jpeg" , ".gif" , ".bmp" , ".tiff" , ".ico" } ;
33
+ private static readonly string SvgExtension = ".svg" ;
30
34
31
35
public static async Task InitializeAsync ( )
32
36
{
@@ -245,6 +249,19 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
245
249
image = GetThumbnail ( path , ThumbnailOptions . ThumbnailOnly ) ;
246
250
}
247
251
}
252
+ else if ( extension == SvgExtension )
253
+ {
254
+ try
255
+ {
256
+ image = LoadFullSvgImage ( path , loadFullImage ) ;
257
+ type = ImageType . FullImageFile ;
258
+ }
259
+ catch ( System . Exception )
260
+ {
261
+ image = Image ;
262
+ type = ImageType . Error ;
263
+ }
264
+ }
248
265
else
249
266
{
250
267
type = ImageType . File ;
@@ -318,7 +335,7 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
318
335
return img ;
319
336
}
320
337
321
- private static BitmapImage LoadFullImage ( string path )
338
+ private static ImageSource LoadFullImage ( string path )
322
339
{
323
340
BitmapImage image = new BitmapImage ( ) ;
324
341
image . BeginInit ( ) ;
@@ -327,24 +344,24 @@ private static BitmapImage LoadFullImage(string path)
327
344
image . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
328
345
image . EndInit ( ) ;
329
346
330
- if ( image . PixelWidth > 320 )
347
+ if ( image . PixelWidth > FullImageSize )
331
348
{
332
349
BitmapImage resizedWidth = new BitmapImage ( ) ;
333
350
resizedWidth . BeginInit ( ) ;
334
351
resizedWidth . CacheOption = BitmapCacheOption . OnLoad ;
335
352
resizedWidth . UriSource = new Uri ( path ) ;
336
353
resizedWidth . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
337
- resizedWidth . DecodePixelWidth = 320 ;
354
+ resizedWidth . DecodePixelWidth = FullImageSize ;
338
355
resizedWidth . EndInit ( ) ;
339
356
340
- if ( resizedWidth . PixelHeight > 320 )
357
+ if ( resizedWidth . PixelHeight > FullImageSize )
341
358
{
342
359
BitmapImage resizedHeight = new BitmapImage ( ) ;
343
360
resizedHeight . BeginInit ( ) ;
344
361
resizedHeight . CacheOption = BitmapCacheOption . OnLoad ;
345
362
resizedHeight . UriSource = new Uri ( path ) ;
346
363
resizedHeight . CreateOptions = BitmapCreateOptions . IgnoreColorProfile ;
347
- resizedHeight . DecodePixelHeight = 320 ;
364
+ resizedHeight . DecodePixelHeight = FullImageSize ;
348
365
resizedHeight . EndInit ( ) ;
349
366
return resizedHeight ;
350
367
}
@@ -354,5 +371,44 @@ private static BitmapImage LoadFullImage(string path)
354
371
355
372
return image ;
356
373
}
374
+
375
+ private static ImageSource LoadFullSvgImage ( string path , bool loadFullImage = false )
376
+ {
377
+ // Set up drawing settings
378
+ var desiredHeight = loadFullImage ? FullImageSize : SmallIconSize ;
379
+ var drawingSettings = new WpfDrawingSettings
380
+ {
381
+ IncludeRuntime = true ,
382
+ // Set IgnoreRootViewbox to false to respect the SVG's viewBox
383
+ IgnoreRootViewbox = false
384
+ } ;
385
+
386
+ // Load and render the SVG
387
+ var converter = new FileSvgReader ( drawingSettings ) ;
388
+ var drawing = converter . Read ( path ) ;
389
+
390
+ // Calculate scale to achieve desired height
391
+ var drawingBounds = drawing . Bounds ;
392
+ var scale = desiredHeight / drawingBounds . Height ;
393
+ var scaledWidth = drawingBounds . Width * scale ;
394
+ var scaledHeight = drawingBounds . Height * scale ;
395
+
396
+ // Convert the Drawing to a Bitmap
397
+ var drawingVisual = new DrawingVisual ( ) ;
398
+ using DrawingContext drawingContext = drawingVisual . RenderOpen ( ) ;
399
+ drawingContext . PushTransform ( new ScaleTransform ( scale , scale ) ) ;
400
+ drawingContext . DrawDrawing ( drawing ) ;
401
+
402
+ // Create a RenderTargetBitmap to hold the rendered image
403
+ var bitmap = new RenderTargetBitmap (
404
+ ( int ) Math . Ceiling ( scaledWidth ) ,
405
+ ( int ) Math . Ceiling ( scaledHeight ) ,
406
+ 96 , // DpiX
407
+ 96 , // DpiY
408
+ PixelFormats . Pbgra32 ) ;
409
+ bitmap . Render ( drawingVisual ) ;
410
+
411
+ return bitmap ;
412
+ }
357
413
}
358
414
}
0 commit comments