Frequently Asked Question:
Clearing memory?
I am currently looking for a PDF library that my company can use. I have built a simple application that displays a single page PDF on a WinForm and what I have noticed is as I re-size my form the memory usage increments and will eventually run out of memory. Is there some way to clear the memory? I noticed in adobe reader it does the same thing but after the memory gets to a certain range adobe clears it some how and the memory usage drops back. Can QuickPDF do the same thing? I am using Windows XP and VB.Net 2010. Following is the code I am using:
Imports System.Runtime.InteropServices Imports QuickPDFAX0726
Public Class Form1
Private Enum SizeEnum
FitToWidth
FitToHeight
FitToPage
ZoomIn
End Enum
Private _currentSizeMode As SizeEnum
Private _rendering As Boolean
Private _qp As PDFLibrary
Private _fh As Integer
Private _ph As Integer
Private _pages As Integer
Private _curPage As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
_qp = New PDFLibrary
_qp.UnlockKey("<put your license key here>")
If IO.File.Exists("C:\Temp\13724601.pdf") = False Then
MessageBox.Show("File does not exist")
Exit Sub
End If
_fh = _qp.DAOpenFile("C:\Temp\13724601.pdf", "")
_pages = _qp.DAGetPageCount(_fh)
_curPage = 1
_ph = GetPage(_fh, _curPage)
DisplayPage(SizeEnum.FitToPage)
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object,
ByVal e As
System.Windows.Forms.FormClosedEventArgs)
Handles Me.FormClosed
On Error Resume Next
_qp.DACloseFile(_fh)
_qp = Nothing
End Sub
Private Function GetPage(ByVal daFileHandle As Integer, ByVal pageIndex As Integer)
As Integer
Return _qp.DAFindPage(_fh, pageIndex)
End Function
Private Sub DisplayPage(ByVal sizeMode As SizeEnum)
Dim image As Image = Nothing
Dim renderingDPI As Integer
Dim renderingWidthDPI As Integer
Dim renderingHeightDPI As Integer
Dim widthPoints As Double
Dim heightPoints As Double
Try
If _rendering Then Exit Sub
_rendering = True
_currentSizeMode = sizeMode
widthPoints = _qp.PageWidth
heightPoints = _qp.PageHeight
Select Case sizeMode
Case SizeEnum.FitToPage
renderingWidthDPI = Convert.ToInt32(PictureBox1.Width * 72 /
widthPoints)
renderingHeightDPI = Convert.ToInt32(PictureBox1.Height * 72 /
heightPoints)
If renderingWidthDPI < renderingHeightDPI Then
renderingDPI = renderingWidthDPI
Else
renderingDPI = renderingHeightDPI
End If
image = New Bitmap(Convert.ToInt32(widthPoints * renderingDPI / 72),
Convert.ToInt32(heightPoints * renderingDPI / 72))
Using graphx = Graphics.FromImage(image)
Dim dc As System.IntPtr = graphx.GetHdc
_qp.DARenderPageToDC(_fh, _ph, renderingDPI, dc.ToInt32)
graphx.ReleaseHdc(dc)
End Using
PictureBox1.Image = image
End Select
_rendering = False
Catch ex As Exception
_rendering = False
End Try
End Sub
Private Sub PictureBox1_SizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles PictureBox1.SizeChanged
DisplayPage(_currentSizeMode)
End Sub
End Class
Try calling:
image.Dispose() after PictureBox1.Image = image;