Windows Develop Bookmark and Share   
 index > Windows Forms General > Image.SaveAdd => "a generic error occurred in GDI+"
 

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

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 offrames 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.combut 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:

Code Snippet

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

clweeks  Thursday, May 31, 2007 9:28 PM
When I got this error it was a permission problem. Make sure the user has write access to this folder.
ThE_lOtUs  Friday, June 01, 2007 7:20 PM
When I got this error it was a permission problem. Make sure the user has write access to this folder.
ThE_lOtUs  Friday, June 01, 2007 7:20 PM
Yeah, I saw that that was the issue sometimes, but it's not a problem for me. E.g. the Image.Save that fires on the first page (as well as some index-logging) writes to that location just fine.
clweeks  Friday, June 01, 2007 8:43 PM

MY SOLUTION TO: A generic error occurred in GDI+
I'm not sure "WHY" this works ... but it solved my problems.

Caveat: Below uses [ImageFormat.Bmp] If you're reading/writing another format, you'll need to tweak.

Caveat: During the write operation a temporary file named [temporaryToDefeatGDIerror.bmp] is momentarily created, copied then deleted.

Here's a general Function class which does the real work.
------------------------------------------------------------------------------------
class fn
{
public static void SaveImageToFile(string DrvPath, string FileExt, Bitmap bm)
{
string actualDPFEwanted = DrvPath + FileExt;
if (File.Exists(actualDPFEwanted) == true) File.Delete(actualDPFEwanted);
string tempDPFE = DrvPath + "temporaryToDefeatGDIerror.bmp";
if (File.Exists(tempDPFE) == true) File.Delete(tempDPFE);
bm.Save(tempDPFE, ImageFormat.Bmp);
File.Copy(tempDPFE, actualDPFEwanted);
File.Delete(tempDPFE);
}
public static Bitmap GetImageDisconnectedFromFile(string DrvPathFileExt)
{
Image Iret = (Image)new Bitmap(1,1);
if (File.Exists(DrvPathFileExt) == true)
{
FileInfo FI = new FileInfo(DrvPathFileExt);
if (FI.Length > 1)
{
Stream s = File.Open(DrvPathFileExt, FileMode.Open);
Iret = Image.FromStream(s);
s.Close();
}
}
Bitmap BM = new Bitmap(Iret);
Iret.Dispose();
GC.Collect(0, GCCollectionMode.Forced);
return BM;
}
}
------------------------------------------------------------------------------------
and here's a sample of how I use each fn.call's
------------------------------------------------------------------------------------
private void ReadOne()
{
string DP = String.Format("{0}\\ImageSink\\", Path.GetDirectoryName(Application.ExecutablePath));
if (!Directory.Exists(DP)) Directory.CreateDirectory(DP);
if (File.Exists(DP + "a.bmp") == true) picturebox1.Image = fn.GetImageDisconnectedFromFile(DP + "a.bmp");
}

private void WriteOne()
{
string DP = String.Format("{0}\\ImageSink\\", Path.GetDirectoryName(Application.ExecutablePath));
if (!Directory.Exists(DP)) Directory.CreateDirectory(DP);
if (!(picturebox1.Image == null)) fn.SaveImageToFile(DP, "a.bmp", (Bitmap)picturebox1.Image);
}
------------------------------------------------------------------------------------

Enjoy y'all
Frederick Volking


Fred
Volking  Sunday, April 26, 2009 11:37 PM

You can use google to search for other answers

Custom Search

More Threads

• Disable Tree view checkbox
• Weird Behaviour !
• Print html as it is displaying
• checkedlistbox
• Integraitng Help System in Visual Studio 2005 Windows Forms Application
• Virtual Keyboard For Touchscreen
• Excel Range
• How to read Excel file through C#.NET ?
• Searching a column in datagrid
• TextBox and ContextMenuStrip