Skip to content
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

[Peek] Unsupported File Previewer - Preserve Transparency For File Icons #22650

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,13 @@ public Task<bool> LoadIconPreviewAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

// TODO: Get icon with transparency
IconHelper.GetIcon(Path.GetFullPath(File.Path), out IntPtr hbitmap);

cancellationToken.ThrowIfCancellationRequested();
await Dispatcher.RunOnUiThread(async () =>
{
cancellationToken.ThrowIfCancellationRequested();
var iconBitmap = await GetBitmapFromHBitmapAsync(hbitmap, cancellationToken);
var iconBitmap = await GetBitmapFromHBitmapWithTransparencyAsync(hbitmap, cancellationToken);
IconPreview = iconBitmap;
});
});
Expand Down Expand Up @@ -162,19 +161,21 @@ private bool HasFailedLoadingPreview()
return hasFailedLoadingIconPreview && hasFailedLoadingDisplayInfo;
}

// TODO: Move this to a helper file (ImagePreviewer uses the same code)
private static async Task<BitmapSource> GetBitmapFromHBitmapAsync(IntPtr hbitmap, CancellationToken cancellationToken)
// TODO: Move this to a common helper file and make transparency a parameter (ImagePrevier uses the same code)
private static async Task<BitmapSource> GetBitmapFromHBitmapWithTransparencyAsync(IntPtr hbitmap, CancellationToken cancellationToken)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
var bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
bitmap.MakeTransparent();

var bitmapImage = new BitmapImage();

cancellationToken.ThrowIfCancellationRequested();
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Bmp);
bitmap.Save(stream, ImageFormat.Png);
stream.Position = 0;

cancellationToken.ThrowIfCancellationRequested();
Expand Down