Dispose Bitmap after have converted to BitmapSource

This commit is contained in:
Paddy Xu
2017-05-29 21:25:30 +03:00
parent 3be1c221c5
commit 4dafe7eebe
5 changed files with 17 additions and 20 deletions

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
@@ -37,12 +36,8 @@ namespace QuickLook.Plugin.PDFViewer
return foundElement; return foundElement;
} }
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
public static BitmapSource ToBitmapSource(this Bitmap source) public static BitmapSource ToBitmapSource(this Bitmap source)
{ {
var ip = source.GetHbitmap();
BitmapSource bs = null; BitmapSource bs = null;
try try
{ {
@@ -57,9 +52,9 @@ namespace QuickLook.Plugin.PDFViewer
bs.Freeze(); bs.Freeze();
} }
finally catch
{ {
DeleteObject(ip); // ignored
} }
return bs; return bs;

View File

@@ -11,7 +11,7 @@ namespace QuickLook.Plugin.PDFViewer
if (values.Length < 2) if (values.Length < 2)
throw new Exception("PageIdToImageConverter"); throw new Exception("PageIdToImageConverter");
var zoom = 0.5f; var zoom = 0.3f;
if (parameter != null) if (parameter != null)
float.TryParse((string) parameter, out zoom); float.TryParse((string) parameter, out zoom);
@@ -21,7 +21,13 @@ namespace QuickLook.Plugin.PDFViewer
var pageId = (int) values[1]; var pageId = (int) values[1];
if (pageId < 0) return null; if (pageId < 0) return null;
return handle.GetPage(pageId, zoom).ToBitmapSource(); var bitmap = handle.GetPage(pageId, zoom);
var bs = bitmap.ToBitmapSource();
bitmap.Dispose();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Optimized);
return bs;
} }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)

View File

@@ -23,7 +23,7 @@
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<ListBox x:Name="listThumbnails" Grid.Column="0" VirtualizingPanel.ScrollUnit="Item" <ListBox x:Name="listThumbnails" Grid.Column="0" VirtualizingPanel.ScrollUnit="Item"
ScrollViewer.IsDeferredScrollingEnabled="False" VirtualizingPanel.IsVirtualizing="True"
SelectedIndex="0" SelectedIndex="0"
Focusable="False" Focusable="False"
Background="#00FFFFFF" Background="#00FFFFFF"

View File

@@ -143,7 +143,9 @@ namespace QuickLook.Plugin.PDFViewer
if (!PdfLoaded) if (!PdfLoaded)
return; return;
var image = PdfHandle.GetPage(CurrentPage, ZoomFactor).ToBitmapSource(); var bitmap = PdfHandle.GetPage(CurrentPage, ZoomFactor);
var image = bitmap.ToBitmapSource();
bitmap.Dispose();
pageViewPanelImage.Source = image; pageViewPanelImage.Source = image;
pageViewPanelImage.Width = pageViewPanelImage.Source.Width; pageViewPanelImage.Width = pageViewPanelImage.Source.Width;

View File

@@ -1,7 +1,5 @@
using System; using System.Drawing;
using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
@@ -9,16 +7,12 @@ namespace QuickLook.Plugin.InfoPanel
{ {
public static class Extensions public static class Extensions
{ {
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
public static BitmapSource ToBitmapSource(this Bitmap source) public static BitmapSource ToBitmapSource(this Bitmap source)
{ {
// BitmapSource.Create throws an exception when the image is scanned backward. // BitmapSource.Create throws an exception when the image is scanned backward.
// The Clone() will make it back scanning forward. // The Clone() will make it back scanning forward.
source = (Bitmap) source.Clone(); source = (Bitmap) source.Clone();
var ip = source.GetHbitmap();
BitmapSource bs = null; BitmapSource bs = null;
try try
{ {
@@ -33,9 +27,9 @@ namespace QuickLook.Plugin.InfoPanel
bs.Freeze(); bs.Freeze();
} }
finally catch
{ {
DeleteObject(ip); // ignored
} }
return bs; return bs;