I am trying to import 1000+ Single page JPEG Encoded Tiff and save it to another location. So i will take each jpeg tiff, create MagickImageCollection Object and get the bitmap and based on the bit-depth applying the compression method and re-save it to memory stream and get the binary data for save it to another location. Have a look on below code snippet for more detail.
Dim InBitMap As Bitmap = Nothing
Using images As New ImageMagick.MagickImageCollection(FilePath)
InBitMap = images.ToBitmap()
End Using
Dim m_Stream As New MemoryStream()
'Get the Encoder and its codec info
Dim CodecInfo As ImageCodecInfo = GetEncoderInfo("image/tiff")
Dim ImgEncoder As New Encoder(CodecInfo.Clsid)
'Encoder Parameters for Compresssion and to denote MultiFrame
Dim EncParms As New EncoderParameters(2)
If InBitMap.PixelFormat = PixelFormat.Format1bppIndexed Then
EncParms.Param(0) = New EncoderParameter(Encoder.Compression, EncoderValue.CompressionCCITT4)
Else
EncParms.Param(0) = New EncoderParameter(Encoder.Compression, EncoderValue.CompressionLZW)
End If
EncParms.Param(1) = New EncoderParameter(Encoder.SaveFlag, EncoderValue.MultiFrame)
'Encoder Parameters for Adding different Frames.
Dim EncAddFrame As New EncoderParameters(1)
EncAddFrame.Param(0) = New EncoderParameter(Encoder.SaveFlag, EncoderValue.FrameDimensionPage)
'Encoder Parameters for Closing Frame.
Dim EncCloseFrame As New EncoderParameters(1)
EncCloseFrame.Param(0) = New EncoderParameter(Encoder.SaveFlag, EncoderValue.Flush)
Dim Dimension As FrameDimension = New FrameDimension(InBitMap.FrameDimensionsList(0))
If InBitMap.GetFrameCount(Dimension) > 1 Then
SyncLock oLock
For ctr As Integer = 0 To InBitMap.GetFrameCount(Dimension) - 1
InBitMap.SelectActiveFrame(Dimension, ctr)
If ctr = 0 Then
InBitMap.Save(m_Stream, CodecInfo, EncParms)
Else
InBitMap.SaveAdd(EncAddFrame)
End If
Next
InBitMap.SaveAdd(EncCloseFrame)
End SyncLock
Else
InBitMap.Save(m_Stream, CodecInfo, EncParms)
End If
m_retBinaryData = m_Stream.ToArray
m_Stream.Close()
m_Stream.Dispose()
InBitMap.Dispose()
After approx 200+ page get saved then it will throw an error while generating bitmap from ToBitmap(). Also, the same error has been observed if we write the images (I.e. MagickImageCollection) to MemoryStream and generating bitmap object from memory Stream.