I have a multipage TIFF file that is 336K before I load it. Once I load it and then turn around and save the file it becomes 950K. I am trying to use CompressionLZW. I get the same size if I use the compression encoder or not. When I load the multipage TIFF file I load an array list of images to be able to manipulate the pages independently. Then I use the following code to save the file to a stream object. Thanks for your help.
Public Sub Save(ByRef p_Stream As Stream)
Dim Main As Image = New Bitmap(CType(_Images(0), Image))
Dim tempFrame As Image
Dim TiffCodecInfo As ImageCodecInfo
Dim SaveEncoder As Encoder
Dim CompressionEncoder As Encoder
Dim Param As EncoderParameter
Dim Params As EncoderParameters
Dim Index As Integer
Get an ImageCodecInfo object that represents the TIFF codec.
TiffCodecInfo = GetEncoderInfo(ImageFormat.Tiff)
Create an Encoder object based on the GUID
for the SaveFlag parameter category.
SaveEncoder = Encoder.SaveFlag
Create an Encoder object based on the GUID
for the Compression parameter category.
CompressionEncoder = Encoder.Compression
Create an EncoderParameters object.
An EncoderParameters object has an array of EncoderParameter
objects. In this case, there is only one
EncoderParameter object in the array.
Params = New EncoderParameters(2)
Param = New EncoderParameter(SaveEncoder, CLng(EncoderValue.MultiFrame))
Params.Param(0) = Param
Param = New EncoderParameter(CompressionEncoder, CLng(EncoderValue.CompressionLZW))
Params.Param(1) = Param
Main.Save(p_Stream, TiffCodecInfo, Params)
Try
For Index = 1 To _Images.Count - 1
tempFrame = New Bitmap(CType(_Images(Index), Image))
Param = New EncoderParameter(SaveEncoder, CLng(EncoderValue.FrameDimensionPage))
Params.Param(0) = Param
Param = New EncoderParameter(CompressionEncoder, CLng(EncoderValue.CompressionLZW))
Params.Param(1) = Param
Main.SaveAdd(tempFrame, Params)
tempFrame.Dispose()
Next
Catch ex As Exception
End Try
Main.Dispose()
Main = Nothing
End Sub
Private Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
Dim index As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
For index = 0 To (encoders.Length - 1)
If format.Guid.Equals(encoders(index).FormatID) Then
Return encoders(index)
End If
Next index
End Function
Public Sub Save(ByRef p_Stream As Stream)
Dim Main As Image = New Bitmap(CType(_Images(0), Image))
Dim tempFrame As Image
Dim TiffCodecInfo As ImageCodecInfo
Dim SaveEncoder As Encoder
Dim CompressionEncoder As Encoder
Dim Param As EncoderParameter
Dim Params As EncoderParameters
Dim Index As Integer
Get an ImageCodecInfo object that represents the TIFF codec.
TiffCodecInfo = GetEncoderInfo(ImageFormat.Tiff)
Create an Encoder object based on the GUID
for the SaveFlag parameter category.
SaveEncoder = Encoder.SaveFlag
Create an Encoder object based on the GUID
for the Compression parameter category.
CompressionEncoder = Encoder.Compression
Create an EncoderParameters object.
An EncoderParameters object has an array of EncoderParameter
objects. In this case, there is only one
EncoderParameter object in the array.
Params = New EncoderParameters(2)
Param = New EncoderParameter(SaveEncoder, CLng(EncoderValue.MultiFrame))
Params.Param(0) = Param
Param = New EncoderParameter(CompressionEncoder, CLng(EncoderValue.CompressionLZW))
Params.Param(1) = Param
Main.Save(p_Stream, TiffCodecInfo, Params)
Try
For Index = 1 To _Images.Count - 1
tempFrame = New Bitmap(CType(_Images(Index), Image))
Param = New EncoderParameter(SaveEncoder, CLng(EncoderValue.FrameDimensionPage))
Params.Param(0) = Param
Param = New EncoderParameter(CompressionEncoder, CLng(EncoderValue.CompressionLZW))
Params.Param(1) = Param
Main.SaveAdd(tempFrame, Params)
tempFrame.Dispose()
Next
Catch ex As Exception
End Try
Main.Dispose()
Main = Nothing
End Sub
Private Function GetEncoderInfo(ByVal format As ImageFormat) As ImageCodecInfo
Dim index As Integer
Dim encoders() As ImageCodecInfo
encoders = ImageCodecInfo.GetImageEncoders()
For index = 0 To (encoders.Length - 1)
If format.Guid.Equals(encoders(index).FormatID) Then
Return encoders(index)
End If
Next index
End Function