diff --git a/src/modules/previewpane/GcodeThumbnailProvider/GcodeThumbnailProvider.cs b/src/modules/previewpane/GcodeThumbnailProvider/GcodeThumbnailProvider.cs index deb619f915..4afe553f3d 100644 --- a/src/modules/previewpane/GcodeThumbnailProvider/GcodeThumbnailProvider.cs +++ b/src/modules/previewpane/GcodeThumbnailProvider/GcodeThumbnailProvider.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 Common.Utilities; namespace Microsoft.PowerToys.ThumbnailHandler.Gcode @@ -47,9 +48,20 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Gcode var gcodeThumbnail = GcodeHelper.GetBestThumbnail(reader); - var thumbnail = gcodeThumbnail?.GetBitmap(); + Bitmap thumbnail = null; - if (thumbnail != null && thumbnail.Width != cx && thumbnail.Height != cx) + try + { + thumbnail = gcodeThumbnail?.GetBitmap(); + } + catch (Exception) + { + // TODO: add logger + } + + if (thumbnail != null && ( + ((thumbnail.Width != cx || thumbnail.Height > cx) && (thumbnail.Height != cx || thumbnail.Width > cx)) || + thumbnail.PixelFormat != PixelFormat.Format32bppArgb)) { // We are not the appropriate size for caller. Resize now while // respecting the aspect ratio. @@ -80,7 +92,7 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Gcode return null; } - Bitmap destImage = new Bitmap(width, height); + Bitmap destImage = new Bitmap(width, height, PixelFormat.Format32bppArgb); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); @@ -95,6 +107,8 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Gcode graphics.DrawImage(image, 0, 0, width, height); } + image.Dispose(); + return destImage; }