title: PDFSharp Export JPG - ASP.NET
body: <p>I am using an ajaxfileupload control to upload a pdf file to the server.  On the server side, I'd like to convert the pdf to jpg.  Using the <a href="http://www.pdfsharp.net/wiki/ExportImages-sample.ashx?AspxAutoDetectCookieSupport=1" rel="nofollow">PDFsharp Sample: Export Images</a> as a guide, I've got the following:</p>
  
  <pre><code>Imports System
  Imports System.Drawing
  Imports System.Drawing.Imaging
  Imports PdfSharp.Pdf
  Imports System.IO
  Imports PdfSharp.Pdf.IO
  Imports PdfSharp.Pdf.Advanced
  
  Namespace Tools
  
  Public Module ConvertImage
  
  Public Sub pdf2JPG(pdfFile As String, jpgFile As String)
  
          pdfFile = System.Web.HttpContext.Current.Request.PhysicalApplicationPath &amp; "upload\" &amp; pdfFile
  
          Dim document As PdfSharp.Pdf.PdfDocument = PdfReader.Open(pdfFile)
  
          Dim imageCount As Integer = 0
          ' Iterate pages
          For Each page As PdfPage In document.Pages
              ' Get resources dictionary
              Dim resources As PdfDictionary = page.Elements.GetDictionary("/Resources")
              If resources IsNot Nothing Then
                  ' Get external objects dictionary
                  Dim xObjects As PdfDictionary = resources.Elements.GetDictionary("/XObject")
                  If xObjects IsNot Nothing Then
                      Dim items As ICollection(Of PdfItem) = xObjects.Elements.Values
                      ' Iterate references to external objects
                      For Each item As PdfItem In items
                          Dim reference As PdfReference = TryCast(item, PdfReference)
                          If reference IsNot Nothing Then
                              Dim xObject As PdfDictionary = TryCast(reference.Value, PdfDictionary)
                              ' Is external object an image?
                              If xObject IsNot Nothing AndAlso xObject.Elements.GetString("/Subtype") = "/Image" Then
                                  ExportImage(xObject, imageCount)
                              End If
                          End If
                      Next
                  End If
              End If
          Next
      End Sub
  
      Private Sub ExportImage(image As PdfDictionary, ByRef count As Integer)
          Dim filter As String = image.Elements.GetName("/Filter")
          Select Case filter
              Case "/DCTDecode"
                  ExportJpegImage(image, count)
                  Exit Select
  
              Case "/FlateDecode"
                  ExportAsPngImage(image, count)
                  Exit Select
          End Select
      End Sub
  
      Private Sub ExportJpegImage(image As PdfDictionary, ByRef count As Integer)
          ' Fortunately JPEG has native support in PDF and exporting an image is just writing the stream to a file.
          Dim stream As Byte() = image.Stream.Value
          Dim fs As New FileStream([String].Format("Image{0}.jpeg", System.Math.Max(System.Threading.Interlocked.Increment(count), count - 1)), FileMode.Create, FileAccess.Write)
          Dim bw As New BinaryWriter(fs)
          bw.Write(stream)
          bw.Close()
      End Sub
  
      Private Sub ExportAsPngImage(image As PdfDictionary, ByRef count As Integer)
          Dim width As Integer = image.Elements.GetInteger(PdfImage.Keys.Width)
          Dim height As Integer = image.Elements.GetInteger(PdfImage.Keys.Height)
          Dim bitsPerComponent As Integer = image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent)
  
          ' TODO: You can put the code here that converts vom PDF internal image format to a Windows bitmap
          ' and use GDI+ to save it in PNG format.
          ' It is the work of a day or two for the most important formats. Take a look at the file
          ' PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.
          ' We don't need that feature at the moment and therefore will not implement it.
          ' If you write the code for exporting images I would be pleased to publish it in a future release
          ' of PDFsharp.
      End Sub
  
  End Module
  
  End Namespace
  </code></pre>
  
  <p>As I debug, it blows up on <code>Dim filter As String = image.Elements.GetName("/Filter")</code> in ExportImage.  The message is:</p>
  
  <blockquote>
    <p>Unhandled exception at line 336, column 21 in ~:46138/ScriptResource.axd?d=LGq0ri4wlMGBKd-1vxLjtxNH_pd26HaruaEG_1eWx-epwPmhNKVpO8IpfHoIHzVj2Arxn5804quRprX3HtHb0OmkZFRocFIG-7a-SJYT_EwYUd--x9AHktpraSBgoZk4VJ1RMtFNwl1mULDLid5o5U9iBcuDi4EQpbpswgBn_oI1&amp;t=ffffffffda74082d
    0x800a139e - JavaScript runtime error: error raising upload complete event and start new upload</p>
  </blockquote>
  
  <p>Any thoughts on what the issue might be?  It seems an issue with the ajaxfileupload control, but I don't understand why it would be barking here.  It's neither here nor there, but I know I'm not using jpgFile yet.  </p>