*I'm using GDPicture.Net 10*
For the purpose of the redesign of one of our application, we decided to give GDPicture a chance. I've implemented it in the new version and it's working quite well. But I have an issue when trying to load a PDF from a different thread. What I do when loading a PDF is the following :
- I create a thumbnail of the page which is kept in memory (I set the DPI to 11 in this case) using this function :
Code: Select all
Private Function CreatePageImage(dpi As Single) As CachedBitmap
_PDFDocument.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitPoint)
Dim gdImage As New GdPicture10.GdPictureImaging
Dim imageID As Integer
Try
imageID = _PDFDocument.RenderPageToGdPictureImage(dpi, False, True)
If imageID = 0 Then Return Nothing
gdImage.ConvertTo24BppRGB(imageID)
Return System.Windows.Media.Imaging.BitmapSource.Create(gdImage.GetWidth(imageID),
gdImage.GetHeight(imageID),
gdImage.GetHorizontalResolution(imageID),
gdImage.GetVerticalResolution(imageID),
PixelFormats.Bgr24, Nothing,
gdImage.GetBits(imageID),
gdImage.GetStride(imageID) * gdImage.GetHeight(imageID),
gdImage.GetStride(imageID))
Finally
If imageID > 0 Then gdImage.ReleaseGdPictureImage(imageID)
gdImage.Dispose()
_PDFDocument.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitInch)
End Try
End Function
Code: Select all
Public Function SavePageImage(index As Integer, fullPath As String) As Boolean
_PDFDocument.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitPoint)
Dim gdImage As New GdPicture10.GdPictureImaging
Dim imageID As Integer = _PDFDocument.RenderPageToGdPictureImage(My.Settings.DPI, False, True)
If imageID = 0 Then Return False
Try
gdImage.ConvertTo24BppRGB(imageID)
Using bmp As System.Drawing.Bitmap = gdImage.GetBitmapFromGdPictureImage(imageID)
bmp.Save(fullPath, System.Drawing.Imaging.ImageFormat.Png)
End Using
Return True
Finally
gdImage.ReleaseGdPictureImage(imageID)
gdImage.Dispose()
_PDFDocument.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitInch)
End Try
End Function
I can deal with the crashing, I just catch the exception the BackgroundWorker is giving me and depending on the error (I get a GDI+ Exception or a NullReferenceException depending on where the thread is when I click cancel) and I deal with it. But sometimes I get an AccessViolationException (the "Attempted to read or write protected memory" error) on the line :
Code: Select all
_PDFDocument.RenderPageToGdPictureImage(My.Settings.DPI, False, True)
Again, I can catch the unhandled exception and show a message to the user idicating there was a problem and then let the App quites, but that's not what I want, I want to be able to catch this exception and deal with it. Meaning cancelling the loading of my pdf and leting the user continue working with my app. Or I want to know if there's a way to keep these functions from crashing when I try cancelling the thread (in this case the problem arises when doing any action while the backgroundWorker is running).
I know it's quite a bizarre problem, but I've spent my entire day on it today and I still don't know why these problems occurs.