MattP
Well-known member
I've run into a situation where I'm trying to resize/compress multipage tiff documents that were scanned at a huge resolution and color depth to a managable file size.
Here's the code I have so far. It works in creating a new file with the reduced resolution but I've run into a couple of snags with compression and color depth.
The parts I'm having issues with
If I change the Compression to any value besides EncoderValue.CompressionLZW I get an error 'parameter is not valid.
If I change the ColorDepth to anything less than 24L I get a message that 'A generic error occurred in GDI+'.
-----
Ideally I'd like to be able to output the files with the same compression and same/lower color depth than the original image. The original compression is CCITT T.6 (which I don't see available) and the color depth can be anywhere from 1 to 32 which I'd like to reduce to B/W or Grayscale.
If anyone can point me in the right direction I'd really appreciate it.
Here's the code I have so far. It works in creating a new file with the reduced resolution but I've run into a couple of snags with compression and color depth.
VB.NET:
Dim fs As FileStream = File.Open("C:\Temp\Big\File2.tif", FileMode.Open, FileAccess.Read)
Dim bmp As Bitmap = CType(Bitmap.FromStream(fs), Bitmap)
Dim resizeRatio As Double = 96 / bmp.HorizontalResolution
Dim ici As ImageCodecInfo = Nothing
For Each info As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
If info.MimeType = "image/tiff" Then
ici = info
End If
Next
Dim ep As New EncoderParameters(3)
ep.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.MultiFrame))
ep.Param(1) = New EncoderParameter(Encoder.Compression, CLng(EncoderValue.CompressionLZW))
ep.Param(2) = New EncoderParameter(Encoder.ColorDepth, 24L)
Dim outFile As Bitmap = Nothing
For i As Integer = 0 To CInt(bmp.GetFrameCount(FrameDimension.Page) - 1)
bmp.SelectActiveFrame(FrameDimension.Page, i)
Dim temp As New Bitmap(CInt(resizeRatio * bmp.Width), CInt(resizeRatio * bmp.Height))
Dim g As Graphics = Graphics.FromImage(temp)
g.DrawImage(bmp, New Rectangle(0, 0, temp.Width, temp.Height), _
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel)
g.Dispose()
If i = 0 Then
outFile = temp
outFile.Save("C:\Temp\Normal\File2.tif", ici, ep)
Else
ep.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.FrameDimensionPage))
outFile.SaveAdd(temp, ep)
End If
If i = CInt(bmp.GetFrameCount(FrameDimension.Page) - 1) Then
ep.Param(0) = New EncoderParameter(Encoder.SaveFlag, CLng(EncoderValue.Flush))
outFile.SaveAdd(ep)
End If
Next
The parts I'm having issues with
VB.NET:
ep.Param(1) = New EncoderParameter(Encoder.Compression, CLng(EncoderValue.CompressionLZW))
ep.Param(2) = New EncoderParameter(Encoder.ColorDepth, 24L)
If I change the Compression to any value besides EncoderValue.CompressionLZW I get an error 'parameter is not valid.
If I change the ColorDepth to anything less than 24L I get a message that 'A generic error occurred in GDI+'.
-----
Ideally I'd like to be able to output the files with the same compression and same/lower color depth than the original image. The original compression is CCITT T.6 (which I don't see available) and the color depth can be anywhere from 1 to 32 which I'd like to reduce to B/W or Grayscale.
If anyone can point me in the right direction I'd really appreciate it.