Click here to Skip to main content
15,918,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I need explanation why my code throw an error (A generic error occurred in GDI+.) while saving a multiple image pages. Sometime it would saved all the pages without any error. At times too it through error on the first. And sometime too the first and second pages would be saved but throw an error when saving any other pages.


C#
public bool SaveMultipleImages(Image[] sourceImages, string targetFile)
{
    bool response = false;

    try
    {
        assignEncoder();

        // Merge individual Images into one Image
        // First page
        Image finalImage = sourceImages[0];

        finalImage.Save(targetFile, tifImageCodecInfo, tifEncoderParametersPage1);
        // All other pages

        int lastpage = sourceImages.Length;
        for (int i = 1; i < sourceImages.Length; i++)
        {
            finalImage.SaveAdd(sourceImages[i], tifEncoderParametersPageX);
        }
        // Last page
        
        finalImage.SaveAdd(tifEncoderParametersPageLast);

        finalImage.Dispose();

        response = true;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error saving merged file: " + ex.Message);
        throw new Exception("Error saving merged file: " + ex.Message);
    }

    return response;
}



C#
private void assignEncoder()
{
   try
   {
       if (encoderAssigned == true)
           return;

       foreach (ImageCodecInfo ici in ImageCodecInfo.GetImageEncoders())
       {
           if (ici.MimeType == "image/tiff")
           {
               tifImageCodecInfo = ici;
           }
       }

       tifEncoderSaveFlag = System.Drawing.Imaging.Encoder.SaveFlag;
       tifEncoderCompression = System.Drawing.Imaging.Encoder.Compression;
       tifEncoderColorDepth = System.Drawing.Imaging.Encoder.ColorDepth;

       tifEncoderParameterMultiFrame = new EncoderParameter(tifEncoderSaveFlag, (long)EncoderValue.MultiFrame);
       tifEncoderParameterFrameDimensionPage = new EncoderParameter(tifEncoderSaveFlag, (long)EncoderValue.FrameDimensionPage);
       tifEncoderParameterFlush = new EncoderParameter(tifEncoderSaveFlag, (long)EncoderValue.Flush);
       tifEncoderParameterCompression = new EncoderParameter(tifEncoderCompression, (long)EncoderValue.CompressionRle);
       tifEncoderParameterLastFrame = new EncoderParameter(tifEncoderSaveFlag, (long)EncoderValue.LastFrame);
       tifEncoderParameter24BPP = new EncoderParameter(tifEncoderColorDepth, (long)24);
       tifEncoderParameter1BPP = new EncoderParameter(tifEncoderColorDepth, (long)8);

       // ******************************************************************* //
       // *** Have only 1 of the following 3 groups assigned for encoders *** //
       // ******************************************************************* //

       // Regular
       tifEncoderParametersPage1 = new EncoderParameters(1);
       tifEncoderParametersPage1.Param[0] = tifEncoderParameterMultiFrame;
       tifEncoderParametersPageX = new EncoderParameters(1);
       tifEncoderParametersPageX.Param[0] = tifEncoderParameterFrameDimensionPage;
       tifEncoderParametersPageLast = new EncoderParameters(1);
       tifEncoderParametersPageLast.Param[0] = tifEncoderParameterFlush;

       encoderAssigned = true;
   }
   catch (Exception ex)
   {
       Console.WriteLine(ex.Message);
       throw ex;
   }
}
Posted
Updated 3-Sep-14 0:36am
v2
Comments
George Jonsson 3-Sep-14 6:37am    
On which line does the error occur?

1 solution

Skeet has addressed this problem here[^].

/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900