diff --git a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs index 2a5a8f2eae..7c305d2c3e 100644 --- a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs +++ b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs @@ -2,6 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Drawing.Drawing2D; +using System.Drawing.Imaging; using System.Globalization; using System.Reflection; using System.Runtime.CompilerServices; @@ -116,6 +117,7 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg _browser.Visible = true; _browser.Width = (int)cx; _browser.Height = (int)cx; + _browser.DefaultBackgroundColor = Color.Transparent; _browser.NavigationCompleted += async (object sender, CoreWebView2NavigationCompletedEventArgs args) => { var a = await _browser.ExecuteScriptAsync($"document.getElementsByTagName('svg')[0].viewBox;"); @@ -258,7 +260,7 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg return null; } - Bitmap destImage = new Bitmap(width, height); + Bitmap destImage = new Bitmap(width, height, PixelFormat.Format32bppArgb); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); @@ -270,10 +272,11 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Svg graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; - graphics.Clear(Color.White); graphics.DrawImage(image, 0, 0, width, height); } + image.Dispose(); + return destImage; } diff --git a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs index 2154585cfd..bb742a14e7 100644 --- a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs +++ b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs @@ -4,6 +4,7 @@ using System; using System.Drawing; +using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Text; @@ -221,5 +222,44 @@ namespace SvgThumbnailProviderUnitTests Assert.IsTrue(bitmap != null); } + + [TestMethod] + public void ResizeImageShouldPreserveAlphaChannel() + { + Bitmap source = new Bitmap(64, 64, PixelFormat.Format32bppArgb); + using (var graphics = Graphics.FromImage(source)) + { + graphics.CompositingMode = CompositingMode.SourceCopy; + graphics.Clear(Color.FromArgb(128, 255, 0, 0)); + } + + using Bitmap resized = SvgThumbnailProvider.ResizeImage(source, 32, 32); + + Assert.IsNotNull(resized); + Assert.AreEqual(PixelFormat.Format32bppArgb, resized.PixelFormat); + + Color center = resized.GetPixel(resized.Width / 2, resized.Height / 2); + Assert.AreEqual(128, center.A, "Resizing must not force the output to be opaque."); + } + + [TestMethod] + public void GetThumbnailShouldPreserveTransparentBackground() + { + var svgBuilder = new StringBuilder(); + svgBuilder.AppendLine(""); + svgBuilder.AppendLine("\t"); + svgBuilder.AppendLine("\t"); + svgBuilder.AppendLine(""); + + SvgThumbnailProvider svgThumbnailProvider = new SvgThumbnailProvider(null); + svgThumbnailProvider.SvgContents = svgBuilder.ToString(); + svgThumbnailProvider.SvgContentsReady.Set(); + + using Bitmap thumbnail = svgThumbnailProvider.GetThumbnail(256); + + Assert.IsNotNull(thumbnail); + Assert.AreEqual(PixelFormat.Format32bppArgb, thumbnail.PixelFormat); + Assert.AreEqual(0, thumbnail.GetPixel(0, 0).A, "The thumbnail background must stay transparent."); + } } }