Avatar of zimmer9
zimmer9
Flag for United States of America asked on

I execute a C# console application that reads binary file & when program saves ouput files, it causes error message "Exception reported: System.Runtime.InteropServices.ExternalException (0x80004005)"

I am writing a C# console application and when the following statement executes to save the bitmap file, I get an error message as follows: Do you know how to resolve this error?

tiff.Save(filenamerev, codec, parameters);   <----   Line 62

---------------

Exception reported: System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, Encoder Parameters encoderParams) at ReadBinary.Extensions.MergeImages(IEnumerable'1 images, FileInfo file) in U:\Visual Studio 2010\Projects\C1\Program.cs:line 62

----------------

line 62 ->  tiff.Save(filenamerev, codec, parameters);
-----------------------------------------------------------------------------

Program snippet:

            try
            {
                codec = (from _codec in ImageCodecInfo.GetImageEncoders() where _codec.MimeType.Equals("image/tiff", StringComparison.OrdinalIgnoreCase) select _codec).FirstOrDefault();
                if (codec != default(ImageCodecInfo))
                {                    
                    parameters = new EncoderParameters(2);
                    parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                    parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                    
                    MyGlobals.countnum = 0;

                    foreach (Image image in images)
                    {
                        MyGlobals.countnum = MyGlobals.countnum + 1;
                        MyGlobals.sequencenum = MyGlobals.countnum.ToString("D8");

                        string filenamerev = MyGlobals.BASE_DIR + "C.C.CHKIMAGE.CHKIMAGE." + MyGlobals.sequencenum + "." + DateTime.Now.ToString("yyyyMMdd") + ".ard.out"; 

                        stream = new MemoryStream();
                        image.Save(stream, ImageFormat.Tiff);

                        tiff = Image.FromStream(stream);

                        tiff.Save(filenamerev, codec, parameters);  <---    Line 62
                       
                    }

                    parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                    tiff.SaveAdd(parameters);      
                }
            }

Open in new window

C#.NET Programming

Avatar of undefined
Last Comment
zimmer9

8/22/2022 - Mon
ste5an

Wrong parameters?
zimmer9

ASKER
This program works fine on a different drive using the same input file.

I did read the following:

You cannot save the same Bitmap which you load into the object as it is being used by another process at the same time.

So first rename it and then try to save it.
ASKER CERTIFIED SOLUTION
Umar Topia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Karrtik Iyer

Can you please try this instead?
            try
            {
                codec = (from _codec in ImageCodecInfo.GetImageEncoders() where _codec.MimeType.Equals("image/tiff", StringComparison.OrdinalIgnoreCase) select _codec).FirstOrDefault();
                if (codec != default(ImageCodecInfo))
                {                    
                    parameters = new EncoderParameters(2);
                    parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                    parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                    
                    MyGlobals.countnum = 0;

                    foreach (Image image in images)
                    {
                        MyGlobals.countnum = MyGlobals.countnum + 1;
                        MyGlobals.sequencenum = MyGlobals.countnum.ToString("D8");

                        string filenamerev = MyGlobals.BASE_DIR + "C.C.CHKIMAGE.CHKIMAGE." + MyGlobals.sequencenum + "." + DateTime.Now.ToString("yyyyMMdd") + ".ard.out"; 

                        using (var mstream = new MemoryStream()) //To ensure Memory stream is present while save is done
{
                        image.Save(mstream , ImageFormat.Tiff);

                        using (var mytiff = Image.FromStream(mstream ))
{

                        mytiff .Save(filenamerev, codec, parameters);  
}
 }                      
                    }

                    parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                    tiff.SaveAdd(parameters);      
                }
            }

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ste5an

btw,  MyGlobals???
zimmer9

ASKER
Hi Karrtik Iyer,

I tried your code and basically get the same error:

Exception reported: System.Runtime.InteropServices.ExternalException (0x80004005); A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, Encoder Paramaters encoderParams) at ReadBinary.Extensions.MergeImages(IEnumerable'1 images, FileInfo file) in U:\Visual Studio 2010\Projects\C1\Program.cs:line  99

Line 99   ->  mytiff.Save(filenamerev, codec, parameters);
Karrtik Iyer

Thanks Zimmer, one more try with the below code (just trying to see if a new Bitmap with the same image resolves the issue). If this does not work can you please post back the new modified code as per below with which you are still getting the error?
            try
            {
                codec = (from _codec in ImageCodecInfo.GetImageEncoders() where _codec.MimeType.Equals("image/tiff", StringComparison.OrdinalIgnoreCase) select _codec).FirstOrDefault();
                if (codec != default(ImageCodecInfo))
                {                    
                    parameters = new EncoderParameters(2);
                    parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                    parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                    
                    MyGlobals.countnum = 0;

                    foreach (Image image in images)
                    {
                        MyGlobals.countnum = MyGlobals.countnum + 1;
                        MyGlobals.sequencenum = MyGlobals.countnum.ToString("D8");

                        string filenamerev = MyGlobals.BASE_DIR + "C.C.CHKIMAGE.CHKIMAGE." + MyGlobals.sequencenum + "." + DateTime.Now.ToString("yyyyMMdd") + ".ard.out"; 
                        using (Bitmap bmp = new Bitmap(image))//Karrtik: To see if creating a new Bitmap from underlying image resolves the issues
                        {
                          using (var mstream = new MemoryStream()) //To ensure Memory stream is present while save is done
                          {
                            bmp.Save(mstream , ImageFormat.Tiff);

                            using (var mytiff = Image.FromStream(mstream ))
                            {
                              mytiff .Save(filenamerev, codec, parameters);  
                            }
                          }
                        }                      
                    }

                    parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                    tiff.SaveAdd(parameters);      
                }
            }

                                          

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Karrtik Iyer

Hi Zimmer, would be good to know what was the problem and how did you resolve it?
zimmer9

ASKER
I do not have permission on the v drive and without permission I cannot access the v drive.
I can not execute code on the v drive because its a File server location.  The purpose of the v drive is to purely store and retrieve files.