Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Merging TIFF Images

Rate me:
Please Sign up or sign in to vote.
3.77/5 (4 votes)
22 Nov 2021CPOL1 min read 8.8K   131   8   1
Create a multipage TIFF image from images
A TIFF image can contain one or more images. In this tip, you will see how to merge multiple images using VB.NET.

Image 1

Introduction

In this period, PDF is used. There was a time when TIFF was also used. This Visual Basic application solves the problem of creating a multipage TIFF image from an ordered series of single TIFF images.

It should be remembered that TIFFs can be compressed, this article does not consider this aspect. The reference framework for this project code is .NET 4.8.

Using the Code

Two main functions are used in the project code: saveImageTIFF() and GetEncoderInfo(). The code is relatively simple. A Button1 button is used to choose the TIFFs to be used, selecting the first one and then the last one. There is no multiselect by choice as, by mistake, we may select the images not in the desired order. Button2 and Button3 respectively add and remove the full path of the image to a Listbox always in order. Button4 calls the saveImageTIFF() function. The TIFF image information is retrieved by the GetEncoderInfo ("image / tiff") function which basically retrieves the correct TIFF codec from a set of codecs. The last sub shows the images selected in the Listbox inside the PictureBox.

The Full Code!

VB.NET
//
Imports System.Drawing.Imaging
Imports System.IO.File
Public Class Form1
    Dim ThisTIFF As String = ""
    Dim Counter As Integer = 0
    Dim objfile As System.IO.File

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Open, show and add the TIFF in listbox1
        OpenFileDialog1.Filter = "TIF Files|*.tif|TIFF Files|*.tiff"
        OpenFileDialog1.FileName = ""

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName.ToString)
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            ThisTIFF = OpenFileDialog1.FileName.ToString
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Add a TIFF
        ListBox1.Items.Add(ThisTIFF)
        Counter += 1
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' Remove a tiff
        If Counter <> 0 Then
            ListBox1.Items.RemoveAt(Counter - 1)
            Counter -= 1
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ' Save the Multi TIFF
        saveImageTIFF()
    End Sub

    Private Sub saveImageTIFF()
        'Save function
        Dim strFileName As String = ""
        SaveFileDialog1.AddExtension = True
        SaveFileDialog1.ShowDialog()
        strFileName = SaveFileDialog1.FileName().ToString

        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

        ' Take the first frame setting it in PictureBox1
        PictureBox1.Image = Image.FromFile(ListBox1.Items.Item(0).ToString)
        Dim saveTif As Bitmap = New Bitmap(PictureBox1.Image)

        Dim myImageCodecInfo As ImageCodecInfo
        Dim myEncoder As Encoder
        Dim myEncoderParameter As EncoderParameter
        Dim myEncoderParameters As EncoderParameters

        'Take the information of the TIFF codec
        myImageCodecInfo = GetEncoderInfo("image/tiff")

        'Create a GUID-based encoder object
        'for the SaveFlag parameter
        myEncoder = Encoder.SaveFlag
        'Create an Encoderparameters object.
        'This has an array of Encoderparameters.
        'In this case, only one encoder
        'EncoderParameter object in the array.
        myEncoderParameters = New EncoderParameters(1)

        myEncoderParameter = New EncoderParameter(myEncoder, CLng(EncoderValue.MultiFrame))
        myEncoderParameters.Param(0) = myEncoderParameter
        saveTif.Save(strFileName, myImageCodecInfo, myEncoderParameters)

        Dim i As Integer
        ListBox1.Items.RemoveAt(0)
        Dim frameCount = ListBox1.Items.Count
        Dim pageNO
        'Remove the first frame, otherwise it would be duplicated.
        Try
            For i = 0 To frameCount - 1

                pageNO = ListBox1.Items.Item(i)
                PictureBox1.Image = Image.FromFile(ListBox1.Items.Item(i))

                If objfile.Exists(strFileName) Then
                    Dim saveFrame As Bitmap = New Bitmap(PictureBox1.Image)

                    myEncoderParameter = New EncoderParameter_
                                         (myEncoder, CLng(EncoderValue.FrameDimensionPage))
                    myEncoderParameters.Param(0) = myEncoderParameter
                    saveTif.SaveAdd(saveFrame, myEncoderParameters)
                    saveFrame.Dispose()
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        saveTif.Dispose()
        Me.Cursor = System.Windows.Forms.Cursors.Default

        MsgBox("File salvato come " & strFileName)
    End Sub

    'GetEncoderInfo
    Private Shared Function GetEncoderInfo(ByVal mimeType As [String]) As ImageCodecInfo
        Dim i As Integer
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()

        For i = 0 To (encoders.Length - 1)
            If (encoders(i).MimeType = mimeType) Then
                Return encoders(i)
            End If
        Next i
    End Function

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
            Handles ListBox1.SelectedIndexChanged
        ' Show the selected TIFF in PictureBox1
        PictureBox1.Image = Image.FromFile(ListBox1.SelectedItem.ToString)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End Sub
End Class

Points of Interest

I hope this tip will be of help to people wishing to become familiar with TIFFs. Remember the size of the final TIFF image depends on the sum of all the images used, the smaller they are, the lower the final weight.

History

  • 21st November, 2021: First release and this article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mike Town24-Nov-21 4:51
Mike Town24-Nov-21 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.