Convert PDF httpPostedFile to JPG Base64 AND save JPG on disk

Apr 9, 2014 at 4:09 PM
Edited Apr 9, 2014 at 4:13 PM
On the same page, I need to convert the PDF to Base64 JPG AND save the JPG to disk

Code to convert to Base64 which works fine.
Using image As New MagickImage(file.InputStream)
    Using ms As New MemoryStream()
       image.Format = MagickFormat.Jpeg
       image.Density = 400
       image.Write(ms)
       imgString = ImageToBase64(Drawing.Image.FromStream(ms), System.Drawing.Imaging.ImageFormat.Jpeg)
    End Using
End Using
Convert same PDF httpPostedFile to a JPG and save on disk - NOT WORKING.
The code below gives me the error message:
An exception of type 'ImageMagick.MagickMissingDelegateErrorException' occurred in Magick.NET-x86.dll but was not handled in user code

Additional information: Magick: no decode delegate for this image format `' @ error/blob.c/BlobToImage/359
Using image1 As New MagickImage(file.InputStream)
     image1.Format = MagickFormat.Jpeg
     image1.Density = 400
     image1.Write(Server.MapPath("Scan/") & folderName & "/" & fileRecNumber & "_" & fileMonth & "_" & fileYear & "_" & imgId & ".jpg")
End Using
Coordinator
Apr 10, 2014 at 10:46 AM
Edited Apr 11, 2014 at 11:35 AM
I cannot explain why your second example is not working. I made a fix in ImageMagick to print a better error message. You should try again after the next release that will be published this week.

You can also rewrite your first example. MagickImage contains a method called ToBase64 and you should set the Density before loading your image:
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(400, 400);
using(MagickImage image = new MagickImage(file.InputStream, settings)
{
   image.Format = MagickFormat.Jpeg;
   imgString = image.ToBase64();
}
Edit: The new version has been publish, can you try it again?
Apr 11, 2014 at 2:07 PM
Edited Apr 11, 2014 at 3:03 PM
I have re-factored my code so I'll save the JPEG on disk and convert to Base64 (thanks for the ToBase64 method!) on the same Using...
Dim settings As New MagickReadSettings()
     settings.Density = New MagickGeometry(400, 400)

     Using image As New MagickImage(file.InputStream, settings)
          Using ms As New MemoryStream()
               image.Format = MagickFormat.Jpeg
               image.Write(ms)
               imgString = image.ToBase64
               pathImg = Server.MapPath("Scan/") & folderName & "/" & fileRecNumber & "_" & fileMonth & "_" & fileYear & "_" & imgId & ".jpg"
               image.Write(pathImg)
    End Using
End Using
Thanks you your help.