Monday, July 29, 2013

Silverlight Convert Tiff to Bitmap Image File

I know you all have had the issue of working with tiff image files in Silverlight. I wanted to share some code today that may help you all in this process. In the SilverlightApp.Web Project please add a silverlight enabled web service and add the following code.

 <OperationContract()> _
    Public Function ConvertTiffToJpegBinary(ByVal imageData As Byte()) As List(Of Byte())
        Using ms As New System.IO.MemoryStream(imageData)
 
            Using imagefile As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
 
                Dim PictureList As New List(Of Byte())
 
                Dim frameDimensions As FrameDimension = New FrameDimension(imagefile.FrameDimensionsList(0))
 
                Dim frameNum As Integer = imagefile.GetFrameCount(frameDimensions)
 
                For frame As Integer = 0 To frameNum - 1
 
                    imagefile.SelectActiveFrame(frameDimensions, frame)
        
                    Using bmp As Bitmap = New Bitmap(imagefile.GetThumbnailImage(768, 1024, Nothing, System.IntPtr.Zero))
 
                        Using bmpStream As New System.IO.MemoryStream
 
                            bmp.Save(bmpStream, System.Drawing.Imaging.ImageFormat.Jpeg)
 
                            bmpStream.Position = 0
 
                            PictureList.Add(bmpStream.ToArray)
 
                        End Using
 
                    End Using
 
                    frame += 1
 
                Next
 
                Return PictureList
 
            End Using
        End Using
    End Function

After adding this code to the service below is the code on how to convert that binary data to a bitmap object to be rendered within Silverlight.

This code actually calls a service to receive the tiff byte array it then sends that information to the service specified above.

  Private Sub btnclick_Click(sender As Object, e As RoutedEventArgsHandles btnclick.Click
        Dim oasisSrv As New OasisSrv.oasiswsObjClient
        AddHandler oasisSrv.testsalesorderCompleted, Sub(Sender1 As Object, e1 As OasisSrv.testsalesorderCompletedEventArgs)
 
                                                         Dim service1client As New ServiceReference1.PortalTiffConversionServiceClient
 
                                                         AddHandler service1client.ConvertTiffToJpegBinaryCompleted, Sub(sender11 As Object, e11 As ServiceReference1.ConvertTiffToJpegBinaryCompletedEventArgs)
 
                                                                                                                         For Each rec In e11.Result
 
                                                                                                                             Using ms As New MemoryStream(rec)
 
                                                                                                                                 Dim bmp As New BitmapImage()
 
                                                                                                                                 bmp.SetSource(ms)
 
                                                                                                                                 Dim image As New Image With {.Source = bmp}
 
                                                                                                                                 Dim scroll As New ScrollViewer
 
                                                                                                                                 Dim panel As New StackPanel
 
                                                                                                                                 scroll.Content = panel
 
                                                                                                                                 panel.Children.Add(image)
 
                                                                                                                                 LayoutRoot.Children.Add(scroll)
 
                                                                                                                             End Using
 
 
                                                                                                                         Next
                                                                                                                     End Sub
                                                         service1client.ConvertTiffToJpegBinaryAsync(e1.opImage)
 
                                                     End Sub
        oasisSrv.testsalesorderAsync("")