Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Multipage Tiff in color!

5.00/5 (5 votes)
24 Jun 2011CPOL 20.2K  
I came upon the need to save multiple frames to a single file - here is where the Tiff file format shines. Not much documentation on how to do it with color so I thought I would share what I found. Enjoy!

VB
Private Sub SaveImages(ByVal imgs() As Image, ByVal filepath As String)
    'get the codec
    Dim info As ImageCodecInfo = Nothing
    For Each ici As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
      If ici.MimeType = "image/tiff" Then
        info = ici
      End If
    Next
    'set the encoding
    Dim enc As Imaging.Encoder = Imaging.Encoder.SaveFlag
    Dim ep As New EncoderParameters(1)
    ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))
    Dim pages As Bitmap = Nothing
    Dim frame As Integer = 0
    For Each img As Image In images
      If frame = 0 Then
        pages = DirectCast(img, Bitmap)
        'save first
        pages.Save(filepath, info, ep)
      Else
        'save next
        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))
        Dim bm As Bitmap = DirectCast(img, Bitmap)
        pages.SaveAdd(bm, ep)
      End If
      If frame = images.Count - 1 Then
        'close.
        ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))
        pages.SaveAdd(ep)
      End If
      frame += 1
    Next
  End Sub

License

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