Parameter is not valid.

Sep 11, 2014 at 3:06 PM
Edited Sep 11, 2014 at 5:55 PM
I am currently using Ghostscript.net to process pdf's into images. For multipage document the first page processes then the next throws the error: "Parameter is not valid." Here is the stack trace:

at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format, BitmapData bitmapData)
at System.Drawing.Bitmap.LockBits(Rectangle rect, ImageLockMode flags, PixelFormat format)
at Ghostscript.NET.Viewer.GhostscriptViewerImage.Lock()
at Ghostscript.NET.Viewer.GhostscriptViewerDisplayHandler.Page(IntPtr handle, IntPtr device, Int32 copies, Int32 flush)
at Ghostscript.NET.GhostscriptDisplayDeviceHandler.display_page(IntPtr handle, IntPtr device, Int32 copies, Int32 flush)

And here is the code:
Dim TestGWScriptRast As New Ghostscript.NET.Rasterizer.GhostscriptRasterizer
Dim GSLatestVer As Ghostscript.NET.GhostscriptVersionInfo =   Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion()
        Try
            Dim OutPutDir As String = My.Computer.FileSystem.SpecialDirectories.Temp
            TestGWScriptRast.Open(PDFPath, GSLatestVer, True)
            For PageToProc As Integer = 1 To TestGWScriptRast.PageCount
                Dim TempPngImagePath As String = OutPutDir & "\temppng" & Identifier & PageToProc & ".png"
                Dim PDFImage As Image = TestGWScriptRast.GetPage(96, 96, PageToProc)
                Dim PDFImageClone As New Bitmap(PDFImage.Width, PDFImage.Height)
                Dim PDFGraphics As Graphics = Graphics.FromImage(PDFImageClone)

                PDFGraphics.DrawImage(PDFImage, 0, 0)
                PDFImage.Dispose()

                PDFImageClone.Save(TempPngImagePath, Imaging.ImageFormat.Png)
                PDFImageClone.Dispose()
                GC.Collect()

            Next
Now I modified the code:
 Dim OutPutDir As String = My.Computer.FileSystem.SpecialDirectories.Temp
            TestGWScriptRast.Open(PDFPath, GSLatestVer, False)

            Dim NumOfPages As Integer = TestGWScriptRast.PageCount
            TestGWScriptRast.Dispose()

            For PageToProc As Integer = 1 To NumOfPages

                Dim InnerScriptRast As New Ghostscript.NET.Rasterizer.GhostscriptRasterizer
                InnerScriptRast.Open(PDFPath, GSLatestVer, False)

                Dim TempPngImagePath As String = OutPutDir & "\temppng" & Identifier & PageToProc & ".png"
                Dim PDFImage As Image = InnerScriptRast.GetPage(96, 96, PageToProc)
                Dim PDFImageClone As New Bitmap(PDFImage.Width, PDFImage.Height)
                Dim PDFGraphics As Graphics = Graphics.FromImage(PDFImageClone)

                Debug.Print(TempPngImagePath)

                PDFImage.Save(TempPngImagePath, ImageFormat.Bmp)

                PDFGraphics.DrawImage(PDFImage, 0, 0)
                PDFImage.Dispose()

                PDFImageClone.Save(TempPngImagePath, Imaging.ImageFormat.Png)
                PDFImageClone.Dispose()

                InnerScriptRast.Dispose()

                GC.Collect()

            Next
And Get this error:
System.Runtime.InteropServices.ExternalException was caught
ErrorCode=-2147467259
Message=A generic error occurred in GDI+.
Source=System.Drawing
StackTrace:
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at CGACommitteeMeetingPacket.CommMeetingSelect.CreateImagesFromPDF(String PDFPath, String Identifier) in C:\DEV\Committees\CGACommApps\CGACommMeetingPacket\CommMeetingSelect.vb:line 1763
InnerException:


Is there a workaround or another possible solution to this issue? Any help is greatly appreciated.
Coordinator
Oct 3, 2014 at 7:55 AM
Hi,

Sorry for the delay.

Can you please provide the PDF you are trying to convert to image?

Cheers,
Josip