From d82c083076df9d13f83e69f29bad9bf32cc24b2c Mon Sep 17 00:00:00 2001 From: Chris Davis Date: Mon, 4 Jan 2021 06:59:53 -0800 Subject: [PATCH] Ensure SVGs are scaled correctly when viewbox is provided (#8872) * Ensure SVGs with hard coded width and height as well as viewbox values are scaled correctly. * Fix misspelling --- .../SvgThumbnailProvider/SvgThumbnailProvider.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs index af09d1369d..73810bbc60 100644 --- a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs +++ b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs @@ -119,6 +119,21 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg var svg = browser.Document.GetElementsByTagName("svg").Cast().FirstOrDefault(); if (svg != null) { + var viewBox = svg.GetAttribute("viewbox"); + if (viewBox != null) + { + // Update the svg style to override any width or height explicit settings + // Setting to 100% width and height will allow to scale to our intended size + // Otherwise, we would end up with a scaled up blurry image. + svg.Style = "width:100%;height:100%"; + + // Wait for the browser to render the content. + while (browser.IsBusy || browser.ReadyState != WebBrowserReadyState.Complete) + { + Application.DoEvents(); + } + } + // Update the size of the browser control to fit the SVG // in the visible viewport. browser.Width = svg.OffsetRectangle.Width;