[Peek]Fix crash while loading thumbnails for small pngs (#28190)

This commit is contained in:
Jaime Bernardo
2023-08-29 12:22:29 +01:00
committed by GitHub
parent 2e74b5dae8
commit e2a3b7dd22

View File

@@ -18,6 +18,7 @@ namespace Peek.FilePreviewer.Previewers.Helpers
public static async Task<BitmapSource> GetBitmapFromHBitmapAsync(IntPtr hbitmap, bool isSupportingTransparency, CancellationToken cancellationToken) public static async Task<BitmapSource> GetBitmapFromHBitmapAsync(IntPtr hbitmap, bool isSupportingTransparency, CancellationToken cancellationToken)
{ {
Bitmap? bitmap = null; Bitmap? bitmap = null;
Bitmap? tempBitmapForDeletion = null;
try try
{ {
@@ -32,7 +33,8 @@ namespace Peek.FilePreviewer.Previewers.Helpers
var transparentBitmap = new Bitmap(bitmapData.Width, bitmapData.Height, bitmapData.Stride, PixelFormat.Format32bppArgb, bitmapData.Scan0); var transparentBitmap = new Bitmap(bitmapData.Width, bitmapData.Height, bitmapData.Stride, PixelFormat.Format32bppArgb, bitmapData.Scan0);
bitmap.Dispose(); // Can't dispose of original bitmap yet as that causes crashes on png files. Saving it for later disposal after saving to stream.
tempBitmapForDeletion = bitmap;
bitmap = transparentBitmap; bitmap = transparentBitmap;
} }
@@ -53,6 +55,7 @@ namespace Peek.FilePreviewer.Previewers.Helpers
finally finally
{ {
bitmap?.Dispose(); bitmap?.Dispose();
tempBitmapForDeletion?.Dispose();
// delete HBitmap to avoid memory leaks // delete HBitmap to avoid memory leaks
NativeMethods.DeleteObject(hbitmap); NativeMethods.DeleteObject(hbitmap);