Search code examples
vb.netsilverlight-5.0visual-studio-lightswitchmemorystream

How do I add an image to a lightswitch silverPDF Document?


Within my Lightswitch desktop app (Silverlight client), I use silverPDF to create an invoice. All works well until I try to add an image (company logo).

My image is one that is saved to the database as image type (byte arrray) via the lightswitch image screen control. As far as I can tell I need to load the byte array into a memory stream then into silverPDF's XImage. Code snippet as follows:

                Dim memStream As New MemoryStream(100)
                memStream.Write(CompDetProp.CompanyLogo, 0, CompDetProp.CompanyLogo.Length)
                Dim myimage As XImage = XImage.FromStream(memStream)
                Dim x As Double = (250 - myimage.PixelWidth * 72 / myimage.HorizontalResolution) / 2
                gfx.DrawImage(myimage, x, 10)

This compiles but I get an exception when run "The byte array is not a recognized imageformat."

I have also tried the following:

                Dim memStream As MemoryStream = New MemoryStream(CompDetProp.CompanyLogo, 0, CompDetProp.CompanyLogo.Length)
                Dim myimage As XImage = XImage.FromStream(memStream)
                Dim x As Double = (250 - myimage.PixelWidth * 72 / myimage.HorizontalResolution) / 2
                gfx.DrawImage(myimage, x, 10)

This second code block has the memory stream closing before it gets used - as far as I can tell.

How can I get the image into a stream that is read by silverPDF XImage before it closes and in the correct format?


Solution

  • After the comment from PDFsharp Expert I found that the image I was using was a .png file. I re saved the image as .jpg and the code worked. I now have an image on my invoice (although size constraints need to be played with to ensure correct fit). Thank you PDFsharp Expert.