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

Multipage Tiff in color!

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
24 Jun 2011CPOL 19.5K   8   3
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)


Written By
Software Developer
United States United States
Programmer of 5+ years. I enjoy VB.Net, WPF and Silverlight, and Web design with ASPX. Live in the beautiful state of Colorado. Enjoy hiking, biking and skiing.

Comments and Discussions

 
Question2011 was a long time ago, but I'll try anyway ... Pin
RedDk22-Nov-21 10:23
RedDk22-Nov-21 10:23 
GeneralMy vote of 5 Pin
Manuele Camilletti19-Apr-12 3:31
professionalManuele Camilletti19-Apr-12 3:31 
Good work
GeneralReason for my vote of 5 Thank you. Pin
Jamal Alqabandi27-Jun-11 23:49
Jamal Alqabandi27-Jun-11 23:49 

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.