Dim comEncoder As Encoder = Encoder.Compression
Dim savEncoder As Encoder = Encoder.SaveFlag
Dim ep As New EncoderParameters(2)
ep.Param(0) = New EncoderParameter(comEncoder, CLng(EncoderValue.CompressionCCITT4))
ep.Param(1) = New EncoderParameter(savEncoder, CLng(EncoderValue.MultiFrame))
Dim codecInfo As ImageCodecInfo = GetCodecInfo("image/tiff")
Dim finalImageName As String = ""
Dim edittedPageName As String = ""
Dim bitmap As Image = Image.FromFile("C:\JCEmailFld\MultiPageCCITT4Compression.tif") 'has indexed pixel format.
MessageBox.Show(bitmap.PixelFormat.ToString)
Dim stamp As Image = Image.FromFile("\\visiflowserver1\visiflow\netDMS\Stamps\DClerkCertifiedCopy.gif")
Dim nonindexedstamp As Image 'object that holds the new nonindex pixel format stamp
'convert stamp to nonindexed pixelformat as well (format24bpprgb), the same format as the first tiff page will be.
If Not stamp.PixelFormat = PixelFormat.Format24bppRgb Then
nonindexedstamp = New Bitmap(stamp.Width, stamp.Height, PixelFormat.Format32bppPArgb)
Dim g As Graphics = Graphics.FromImage(CType(nonindexedstamp, Image))
g.DrawImage(stamp, New Point(0, 0))
g.Dispose()
End If
'keep track of the pages for later combination.
Dim pageFiles As New ArrayList
'Save each page as an individual tiff file.
For i As Integer = 0 To bitmap.GetFrameCount(FrameDimension.Page) - 1
bitmap.SelectActiveFrame(FrameDimension.Page, i)
Dim filename As String = "C:\JCEmailFld\" + Now.ToFileTime.ToString + "_Page" + i.ToString + ".tif"
bitmap.Save(filename, ImageFormat.Tiff)
pageFiles.Add(filename)
Next
bitmap.Dispose()
'get the first page.
Dim firstPage As Bitmap = bitmap.FromFile(pageFiles(0))
Dim edittedFirstPage As Bitmap 'The bitmap we'll store the newly editted first page to.
'if the tif image has an indexed pixel format or an undefined one create a new
'bitmap image with format24bpprgb (or any other format besides indeded so we can
'get a graphics object to draw on).
If firstPage.PixelFormat = PixelFormat.Format1bppIndexed Or _
firstPage.PixelFormat = PixelFormat.Format4bppIndexed Or _
firstPage.PixelFormat = PixelFormat.Format8bppIndexed Or _
firstPage.PixelFormat = PixelFormat.Indexed Or _
firstPage.PixelFormat = PixelFormat.Undefined Then
Dim temp As New Bitmap(firstPage.Width, firstPage.Height, PixelFormat.Format32bppPArgb) 'the new bitmap we will draw the first page to.
'draw the stamp to the first page (this is what is causing the problem for the Bitmap.save(filename,codecinfo,ep) call).
Dim g As Graphics = Graphics.FromImage(CType(temp, Image))
g.DrawImage(CType(firstPage, Image), 0, 0, firstPage.Width, firstPage.Height)
g.DrawImage(nonindexedstamp, CInt(nonindexedstamp.Width / 4), (temp.Height - nonindexedstamp.Height) - CInt(nonindexedstamp.Height / 2), nonindexedstamp.Width, nonindexedstamp.Height)
g.Dispose()
Dim newFileName As String = "C:\JCEmailFld\" + Now.ToFileTime.ToString + "_EdittedPage0.tif"
'I thought saving the file with a tiff format first, the reopening to add the other pages would fix the problem,
'but it didn't.
temp.Save(newFileName, ImageFormat.Tiff)
temp.Dispose()
firstPage.Dispose()
edittedFirstPage = bitmap.FromFile(newFileName)
End If
'Now to convert the image back to an indexed1bbp format
Dim bmdo As BitmapData = edittedFirstPage.LockBits(New Rectangle(0, 0, edittedFirstPage.Width, edittedFirstPage.Height), ImageLockMode.ReadOnly, edittedFirstPage.PixelFormat)
'and the new 1bpp bitmap
Dim bm As New Bitmap(edittedFirstPage.Width, edittedFirstPage.Height, PixelFormat.Format1bppIndexed)
Dim bmdn As BitmapData = bm.LockBits(New Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed)
'for diagnostics
Dim dt As DateTime = DateTime.Now
'scan through the pixels Y by X
Dim y As Integer
For y = 0 To edittedFirstPage.Height - 1
Dim x As Integer
For x = 0 To edittedFirstPage.Width - 1
'generate the address of the colour pixel
Dim index As Integer = y * bmdo.Stride + x * 4
'check its brightness
If Color.FromArgb(Marshal.ReadByte(bmdo.Scan0, index + 2), Marshal.ReadByte(bmdo.Scan0, index + 1), Marshal.ReadByte(bmdo.Scan0, index)).GetBrightness() > 0.5F Then
Me.SetIndexedPixel(x, y, bmdn, True) 'set it if its bright.
End If
Next x
Next y
'tidy up
bm.UnlockBits(bmdn)
edittedFirstPage.UnlockBits(bmdo)
'Save the edittedfirstpage as tiff with codecInfo and Mulitpage/Compression parameters.
bm.Save("C:\JCEmailFld\" + Now.ToFileTime.ToString + "_FinalImage.tif", codecInfo, ep)
ep.Param(1) = New EncoderParameter(savEncoder, CLng(EncoderValue.FrameDimensionPage))
'loop and add the rest of the pages to the tiff file.
For j As Integer = 1 To pageFiles.Count - 1
Dim pageFile As Bitmap = Drawing.Bitmap.FromFile(pageFiles(j))
MessageBox.Show(pageFile.PixelFormat.ToString())
bm.SaveAdd(CType(pageFile, Image), ep)
pageFile.Dispose()
Next
'finally, flush the image.
ep.Param(1) = New EncoderParameter(savEncoder, CLng(EncoderValue.Flush))
bm.SaveAdd(ep)
bm.Dispose()
edittedFirstPage.Dispose()