PdfiumViewer icon indicating copy to clipboard operation
PdfiumViewer copied to clipboard

Insufficient Memory

Open fanghuafan opened this issue 9 years ago • 1 comments

Why WPF use pdfiumviewer will be a lack of memory when I display all the image(PDF) in the panel? My code: xaml:

<ScrollViewer x:Name="verticalScroll" ScrollChanged="verticalScroll_ScrollChanged" Background="AliceBlue" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
	<ScrollViewer.Content>
		<StackPanel x:Name="pdfAll" Orientation="Vertical" Background="Gray">
		</StackPanel>
	</ScrollViewer.Content>
</ScrollViewer>

C#:

public void SetUrlTextBoxEmpty(string value)
{
	int width = (int)this.ActualWidth;
	int height = (int)this.ActualHeight + 1300;
	try
	{
		for (int i = 0; i < pdfDoc.PageCount; i++)
		{
			Image img = new Image();
			Label lbl = new Label();
			lbl.Content = i;
			Thickness thick = new Thickness(0, 0, 0, 10);
			img.Margin = thick;

			tokenSource.Token.ThrowIfCancellationRequested();

			var image = pdfDoc.Render(i, width, height, width, height, false);

			img.Source = BitmapToBitmapSource(image as System.Drawing.Bitmap);

			GC.Collect();

			this.pdfAll.Children.Add(img);
			this.pdfAll.Children.Add(lbl);
		}
	}
	catch (Exception ex)
	{
		tokenSource.Cancel();
		MessageBox.Show(ex.Message);
	}
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);

private BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap bitmap)
{
	IntPtr ptr = bitmap.GetHbitmap();
	BitmapSource result =
		System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
			ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
	//release resource  
	DeleteObject(ptr);

	return result;
}

Thank you!

fanghuafan avatar Nov 17 '16 02:11 fanghuafan

I think the problem is with pdfDoc.Render(i, width, height, width, height, false);. The second width, height is incorrect, and instead should be the horizontal and vertical DPI. This may be the reason for the huge memory allocation.

Can you try pdfDoc.Render(page, width, height, 96, 96, false);?

pvginkel avatar Mar 20 '17 11:03 pvginkel