Wednesday, September 10, 2014

How to merge Multi Pages tiff files into single tiff in

How to merge Multi Pages tiff files into single tiff, or how to convert different images into single tiff file.

You may need to merge multi pages tiff into single tiff file or may be many images into single tiff file. Here I am sharing the code with you.

Private Function MergeImages(ByVal FileList As ArrayList, ByVal Filename As String) As Boolean

            If FileList.Count > 0 Then
                Dim bmpFirstImage As Bitmap = New Bitmap(FileList.Item(0).ToString)
                Dim masterBitmap As Bitmap = Nothing

                If bmpFirstImage IsNot Nothing Then
                    bmpFirstImage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, 0)
                    masterBitmap = bmpFirstImage
                End If

                Dim enc As Imaging.Encoder = Encoder.SaveFlag
                Dim info As ImageCodecInfo = Nothing
                Dim ice As ImageCodecInfo
                For Each ice In ImageCodecInfo.GetImageEncoders()
                    If ice.MimeType = "image/tiff" Then
                        info = ice
                    End If
                Next ice

                Dim ep As New EncoderParameters(1)
                ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))

                masterBitmap.Save(Filename, info, ep)
                ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))

                Dim i As Integer
                    If bmpFirstImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page) > 1 Then
                        For i = 1 To bmpFirstImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page) - 1
                            bmpFirstImage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i)
                            masterBitmap.SaveAdd(bmpFirstImage, ep)
                        Next i
                    End If
                Catch ex As Exception

                End Try

                For index As Integer = 1 To FileList.Count - 1
                    Dim bmpSecondImage As Bitmap
                    bmpSecondImage = New Bitmap(FileList.Item(index).ToString)
                    For i = 0 To bmpSecondImage.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page) - 1
                        bmpSecondImage.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i)
                        masterBitmap.SaveAdd(bmpSecondImage, ep)
                    Next i
                ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))

                Return True
            End If
        Catch ex As Exception
            Return False

        End Try
        Return False
    End Function

Search This Blog