From 685f4ead0e18dbfb4e14535d09ce222ae59924c9 Mon Sep 17 00:00:00 2001 From: "S. M. Mohiuddin Khan Shiam" <147746955+mohiuddin-khan-shiam@users.noreply.github.com> Date: Tue, 17 Jun 2025 12:24:52 +0600 Subject: [PATCH] =?UTF-8?q?Fix=20PDF=20thumbnail=20generation=20=E2=80=93?= =?UTF-8?q?=20add=20`System.Drawing`=20reference=20and=20reset=20stream=20?= =?UTF-8?q?position=20(#40023)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem [PdfThumbnailProvider](cci:1://file:///d:/Github/PowerToys/src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs:15:8-18:9) could fail at compile-time **and/or** produce empty or invalid thumbnails at runtime: 1. [Image](cci:1://file:///d:/Github/PowerToys/src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs:81:8-103:9) / `Bitmap` types come from **System.Drawing**, but the namespace was missing, causing build errors in environments that don’t rely on implicit global usings. 2. After rendering the first PDF page to an in-memory stream, the stream’s cursor remained at the end. `Image.FromStream(stream.AsStream())` therefore read **zero bytes**, throwing an exception or creating an empty bitmap. ## Fix * Added explicit `using System.Drawing;`. * Added `stream.Seek(0);` right before `Image.FromStream(...)` to rewind the stream to the beginning. ```csharp using System.Drawing; // new ... page.RenderToStreamAsync(...).GetAwaiter().GetResult(); stream.Seek(0); // rewind so Image.FromStream reads data imageOfPage = Image.FromStream(stream.AsStream()); ``` ## Testing - Rebuilt the solution – compilation succeeds without missing-namespace errors. - Ran thumbnail handler against multiple PDFs; thumbnails now render correctly and no runtime exceptions are thrown. ## Impact Users regain reliable PDF thumbnail previews in Explorer, improving usability and preventing crashes or blank icons. --------- Co-authored-by: leileizhang --- .../previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs b/src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs index e82607b299..e6b40f0ea8 100644 --- a/src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs +++ b/src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs @@ -1,6 +1,7 @@ -// Copyright (c) Microsoft Corporation +// Copyright (c) Microsoft Corporation // 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; using Windows.Data.Pdf; using Windows.Storage; using Windows.Storage.Streams; @@ -95,6 +96,8 @@ namespace Microsoft.PowerToys.ThumbnailHandler.Pdf DestinationHeight = height, }).GetAwaiter().GetResult(); + stream.Seek(0); + imageOfPage = Image.FromStream(stream.AsStream()); return imageOfPage;