-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Fix PDF thumbnail generation – add System.Drawing
reference and reset stream position
#40023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…et stream position 🔬 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Fixes PDF thumbnail generation by explicitly adding the missing System.Drawing
reference and rewinding the in-memory stream before creating the image.
- Added
using System.Drawing
to resolve missing namespace errors. - Inserted
stream.Seek(0);
to reset the stream position soImage.FromStream(...)
reads data correctly.
Comments suppressed due to low confidence (1)
src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs:99
- Consider wrapping the stream in a
using
statement or explicitly disposing it after use to free resources and avoid potential memory leaks.
stream.Seek(0);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for having the change.
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs
Outdated
Show resolved
Hide resolved
src/modules/previewpane/PdfThumbnailProvider/PdfThumbnailProvider.cs
Outdated
Show resolved
Hide resolved
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
…et stream position (#40023) ## 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 <leilzh@microsoft.com>
Problem
PdfThumbnailProvider could fail at compile-time and/or produce empty or invalid thumbnails at runtime:
Bitmap
types come from System.Drawing, but the namespace was missing, causing build errors in environments that don’t rely on implicit global usings.Image.FromStream(stream.AsStream())
therefore read zero bytes, throwing an exception or creating an empty bitmap.Fix
using System.Drawing;
.stream.Seek(0);
right beforeImage.FromStream(...)
to rewind the stream to the beginning.Testing
Impact
Users regain reliable PDF thumbnail previews in Explorer, improving usability and preventing crashes or blank icons.