Skip to content

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

Merged
merged 3 commits into from
Jun 17, 2025

Conversation

mohiuddin-khan-shiam
Copy link
Contributor

Problem

PdfThumbnailProvider could fail at compile-time and/or produce empty or invalid thumbnails at runtime:

  1. Image / 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.
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.

…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.
Copy link
Contributor

@Copilot Copilot AI left a 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 so Image.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);

@yeelam-gordon yeelam-gordon requested a review from lei9444 June 12, 2025 23:28
@yeelam-gordon yeelam-gordon added the Product-File Explorer Power Toys that touch explorer like Preview Pane label Jun 12, 2025
Copy link
Contributor

@yeelam-gordon yeelam-gordon left a 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.

@lei9444
Copy link
Contributor

lei9444 commented Jun 17, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lei9444
Copy link
Contributor

lei9444 commented Jun 17, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lei9444 lei9444 merged commit f7257bf into microsoft:main Jun 17, 2025
9 checks passed
lei9444 added a commit that referenced this pull request Jun 17, 2025
<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
This is an additional fix that extends the work done in
#40023

fix #39851
yeelam-gordon pushed a commit that referenced this pull request Jun 20, 2025
…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>
yeelam-gordon pushed a commit that referenced this pull request Jun 20, 2025
<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
This is an additional fix that extends the work done in
#40023

fix #39851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Product-File Explorer Power Toys that touch explorer like Preview Pane
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants