Link to home
Create AccountLog in
Avatar of zimmer9
zimmer9Flag 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

Avatar of ste5an
ste5an
Flag of Germany image

Wrong parameters?
Avatar of 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
Avatar of Umar Topia
Umar Topia
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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

btw,  MyGlobals???
Avatar of 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);
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

Hi Zimmer, would be good to know what was the problem and how did you resolve it?
Avatar of 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.