From 8e2f8f44135ccedd8ddda2c7dda9ed91d6f016c0 Mon Sep 17 00:00:00 2001 From: VewDev <36-VewDev@users.noreply.git.ryujinx.app> Date: Mon, 1 Sep 2025 15:07:35 +0200 Subject: [PATCH] feat: implement SVG to PNG conversion for app icon rendering --- src/Ryujinx/UI/Controls/RyujinxLogo.cs | 47 ++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Ryujinx/UI/Controls/RyujinxLogo.cs b/src/Ryujinx/UI/Controls/RyujinxLogo.cs index 972f8f4e0..4d4733fd6 100644 --- a/src/Ryujinx/UI/Controls/RyujinxLogo.cs +++ b/src/Ryujinx/UI/Controls/RyujinxLogo.cs @@ -4,6 +4,7 @@ using Avalonia.Media.Imaging; using Ryujinx.Ava.Common.Models; using Ryujinx.Ava.Systems.Configuration; using Ryujinx.Common; +using SkiaSharp; using Svg.Skia; using System; using System.IO; @@ -62,8 +63,8 @@ namespace Ryujinx.Ava.UI.Controls // SVG files need to be converted to an image first if (selectedIcon.FullPath.EndsWith(".svg", StringComparison.OrdinalIgnoreCase)) { - // TODO: Convert SVG to a bitmap - using SKSvg svg = new(); + Stream pngStream = ConvertSvgToPng(activeIconStream); + CurrentLogoBitmap.Value = new Bitmap(pngStream); } else { @@ -80,5 +81,47 @@ namespace Ryujinx.Ava.UI.Controls } private void WindowIconChanged_Event(object _, ReactiveEventArgs rArgs) => SetNewAppIcon(rArgs.NewValue); + + private static Stream ConvertSvgToPng(Stream svgStream) + { + int width = 256; + int height = 256; + + // Load SVG + var svg = new SKSvg(); + svg.Load(svgStream); + + // Determine size + var picture = svg.Picture; + if (picture == null) + throw new InvalidOperationException("Invalid SVG data"); + + var picWidth = width > 0 ? width : (int)svg.Picture.CullRect.Width; + var picHeight = height > 0 ? height : (int)svg.Picture.CullRect.Height; + + // Create bitmap + using var bitmap = new SKBitmap(picWidth, picHeight); + using var canvas = new SKCanvas(bitmap); + canvas.Clear(SKColors.Transparent); + + // Scale to fit + float scaleX = (float)picWidth / svg.Picture.CullRect.Width; + float scaleY = (float)picHeight / svg.Picture.CullRect.Height; + canvas.Scale(scaleX, scaleY); + + canvas.DrawPicture(svg.Picture); + canvas.Flush(); + + // Encode PNG into memory stream + var outputStream = new MemoryStream(); + using (var image = SKImage.FromBitmap(bitmap)) + using (var data = image.Encode(SKEncodedImageFormat.Png, 100)) + { + data.SaveTo(outputStream); + } + + outputStream.Position = 0; + return outputStream; + } } }