Image.SaveAdd => "a generic error occurred in GDI+"

clweeks

Member
Joined
Feb 21, 2007
Messages
18
Programming Experience
3-5
I've posted this on an MSDN forum also, but I figure casting a wider net is always good and I can report back here if a solution comes from there and vice versa.

So, I'm trying to read through a directory of tiff images, some are single and some are multi-page -- all should be group-4 compressed, and write on the images and save them back out in the same structure (number of frames per file). I thought it was working for a while before discovering that I had to deal with multi-page tiffs.

I'm modeling my code on some from BobPowell.com but I'm not getting it all to come together. I'll post a block of code below, but basically, the image.SaveAdd(Image, EncoderParameter) is throwing a generic error. I've searched around and come up with e.g. these articles and this one and others, but nothing that's solving my problem. I've tried disposing the images at various places and not disposing at all and get no change in results.

It's entirely possible that I've messed stuff up that I had working (the order of assignment of the enParams.Param(0) states, etc. So, feel free to point that out if you see it, but also, know that I can probably get that back to working OK once I have this save problem worked out (unless of course, they're linked).

Here's my code:

VB.NET:
Dim root As New DirectoryInfo("c:\testing\in")

For Each tiffFile As FileInfo In root.GetFiles("*.tif")
tiff = Image.FromFile(tiffFile.FullName)
mptFD = New FrameDimension(tiff.FrameDimensionsList(0))
Dim pageCount As Integer = tiff.GetFrameCount(mptFD)
Dim tiff2 As Image
tiff2 = tiff
enParams.Param(0) = New EncoderParameter(enc, CType(EncoderValue.MultiFrame, Long))
enParams.Param(0) = New EncoderParameter(Encoder.Compression, EncoderValue.CompressionCCITT4)

For i As Integer = 1 To pageCount
Dim tempBM As New Bitmap(tiff.Width, tiff.Height, PixelFormat.Format64bppArgb)
tempBM.SetResolution(tiff.HorizontalResolution, tiff.VerticalResolution)

Using g As Graphics = Graphics.FromImage(tempBM)
g.FillRectangle(Brushes.White, New Rectangle(0, 0, tiff.Width, tiff.Height))
g.DrawImage(tiff, 80, 80, tiff.Width - 160, tiff.Height - 160)
g.DrawString("RCV DATE: " & Now.ToString, displayFont, Brushes.Black, 100, 20)
End Using

tiff2 = convertToG4tiff(tempBM) 'this returns a group-four compressed version

If i = 1 Then
tiff2.Save("c:\testing\out\" & tiffFile.Name, tifCodec, enParams)
'enParams.Param(0) = New EncoderParameter(enc, CType(EncoderValue.FrameDimensionPage, Long))
'tiff2.Dispose()
Else
tiff2.SaveAdd(tiff2, enParams)
'tiff2.Dispose()
End If

Next

enParams.Param(0) = New EncoderParameter(enc, CType(EncoderValue.Flush, Long))
tiff2.SaveAdd(enParams)

Next
Thanks for your time,

--Chris
 
See the post from jo0ls at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1360678&SiteID=1&mode=1, see how both saveParam and compressionParam is used, something like:
EncoderParams.Param(0) = CompressionEncodeParam
EncoderParams.Param(1) = SaveEncodeParam
So you should be able to keep the compressionParam, while saveParam must be set to MultiFrame for first Save and FrameDimensionPage for SaveAdd.
 
Back
Top