Multi-Page Tiff storage
Hello, all
I am using GDI to read multi-page tiff images and extract certain pages to create an additional multi-page tiff document.
I am referencing the active frames as a bitmap as follows:
'get image format from original tiff file
Me._img.SelectActiveFrame(FrameDimension.Page, fromPageIndex)
pages = New Bitmap(Me._img)
and saving using the saveadd method.
The problem is, when saving to a new tiff file, the file page size is 4 times that of the original (page size, not file size). I have been searching for days for a way to extract information from the original tiff and carry it over to the new tiff, but to no avail.
I understand that when you load the page index to a bitmap, you are decompressing it (stemming from my experience with JAI).
Is there a way to recompress using the compression of the original tiff?
My full code is as follows:
'/ <summary>
'/ Saves the specified subpages to the specified file
'/ </summary>
Public Sub savePagesInFile(ByVal filename As String, ByVal fromPageIndex As Integer, ByVal toPageIndex As Integer)
Dim pages As Bitmap = Nothing
Dim LastPage As Integer = toPageIndex
Dim lastPageIndex As Integer = _
Me._img.GetFrameCount(System.Drawing.Imaging.Frame Dimension.Page) - 1
If (fromPageIndex >= 0) Then
' if there are less pages than the specified toPageIndex
' toPageIndex is the last page
If (toPageIndex > lastPageIndex) Then
toPageIndex = lastPageIndex
End If
'get image format from original tiff file
Me._img.SelectActiveFrame(FrameDimension.Page, fromPageIndex)
pages = New Bitmap(Me._img)
'get Image Codec Info
Dim codec As ImageCodecInfo = getTiffCodec()
'get encoder to save
Dim saveEncoder As System.Drawing.Imaging.Encoder = _
System.Drawing.Imaging.Encoder.SaveFlag
'get encoderparam to save multi-frame document
Dim ep As EncoderParameters = New EncoderParameters(1)
ep.Param(0) = _
New EncoderParameter(saveEncoder, CLng(EncoderValue.MultiFrame))
'save first page with Codec Info
pages.Save(filename, codec, ep)
'loop through intended pages
ep.Param(0) = New EncoderParameter(saveEncoder, CLng(EncoderValue.FrameDimensionPage))
Dim bmp As Bitmap
For i As Integer = fromPageIndex + 1 To toPageIndex
'Append each additional page
Me.appendPageToFile(filename, i, pages)
Next
'create flush encoder param
ep.Param(0) = New EncoderParameter(saveEncoder, CLng(EncoderValue.Flush))
'flush image
pages.SaveAdd(ep)
End If
End Sub
'/ <summary>
'/ Gets the ImageCodecInfo for tiff images
'/ </summary>
Protected Function getTiffCodec() As System.Drawing.Imaging.ImageCodecInfo
Dim info As ImageCodecInfo = Nothing
For Each ice As ImageCodecInfo In ImageCodecInfo.GetImageEncoders
If ice.MimeType.ToLower = "image/tiff" Then
info = ice
End If
Next
Return info
End Function
'/ <summary>
'/ Appends the specified page to the file
'/ </summary>
Public Sub appendPageToFile(ByVal fileName As String, ByVal pageIndex As Integer, ByRef pages As Bitmap)
Dim ep As EncoderParameters = New EncoderParameters(1)
'get encoder to save
Dim saveEncoder As System.Drawing.Imaging.Encoder = _
System.Drawing.Imaging.Encoder.SaveFlag
ep.Param(0) = New EncoderParameter(saveEncoder, CLng(EncoderValue.FrameDimensionPage))
Dim bmp As Bitmap
'Append each additional page
Me._img.SelectActiveFrame(FrameDimension.Page, pageIndex)
bmp = New Bitmap(Me._img)
pages.SaveAdd(bmp, ep)
End Sub
Thanks for your help
thanks
__________________
thanks
|