Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am using the below code to get a byte stream and save an image.

but now I am facing a problem. it is throwing an exception. "A generic error occurred in GDI+"

It doesn't give any errors when I build it. if I run the exe manually also it is fine, but when I schedule it in task scheduler (my server is Windows 2008) it gives me this exception.

please find my below code.
public string byteArrayToImage(byte[] byteArrayIn)

                if (byteArrayIn != null)
                    MemoryStream ms = new MemoryStream(byteArrayIn);
                    Image returnImage = Image.FromStream(ms);
                    guid = Guid.NewGuid();
                    _imageName = guid.ToString();
                    _fullImagePath = "images/" + _imageName + ".jpg";
                    returnImage.Save(_fullImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);

            catch (Exception ex)
                UpdateEmailStatus("Sending Failed-byteArrayToImage3" + " " + ex.Message);
            return _fullImagePath;


please help me on this.
I have a production release tomorrow.

Updated 26-Nov-12 5:18am
Mendor81 26-Nov-12 11:08am    
Have you tried a step by step debug? at which line does it throw the exception?
Kasun Koswattha 26-Nov-12 11:10am    

yes. If i debug it is working fine. no exception. this is happening only in production server.

The "A generic error occurred in GDI+" message basically means that whatever your array of bytes contains, it is not recognisable as an image:
Image returnImage = Image.FromStream(ms);
Is failing.

Check your data - have you passed the name of a image file instead of it's content?

I must read questions a bit more carefully!

The chances are that if it works normally, then fails on scheduled execution it isn't the image load that is the problem - it's the save.

Scheduled tasks do not run with the same user as "normal" applications, and there is a very good chance that the folder you are trying to save the file to does not have write permissions for the user that is executing the scheduled task.

Change the folder permissions to "any user full access" and you should be fine.
Share this answer
Kasun Koswattha 26-Nov-12 11:12am    
yes. im passing correct data. if i run the exe manually, it is working fine and image is saving. this is happening when i schedule it. please advice
OriginalGriff 26-Nov-12 11:14am    
See edited answer - I saw the "scheduled" bit just as I pressed the "post message" button! :blush:
Kasun Koswattha 26-Nov-12 11:19am    
Dear, i have given the full permission to the local users. but still no luck. :(
I used this code for image saving i might help you maybe!

using System.Drawing.Imaging;
using System.Drawing;
using System.IO;

public static void SaveJpeg
(string path, Image img, int quality)
EncoderParameter qualityParam
= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

ImageCodecInfo jpegCodec
= GetEncoderInfo(@"image/jpeg");

EncoderParameters encoderParams
= new EncoderParameters(1);

encoderParams.Param[0] = qualityParam;

System.IO.MemoryStream mss = new System.IO.MemoryStream();

System.IO.FileStream fs
= new System.IO.FileStream(path, System.IO.FileMode.Create
, System.IO.FileAccess.ReadWrite);

img.Save(mss, jpegCodec, encoderParams);
byte[] matriz = mss.ToArray();
fs.Write(matriz, 0, matriz.Length);

Share this answer
Kasun Koswattha 26-Nov-12 11:13am    
but if i run it manually instead of scheduling, its working fine, the image is saving.
Mendor81 26-Nov-12 11:17am    
Could be a dump question, but have you checked the path when scheduling the save?
Kasun Koswattha 26-Nov-12 11:19am    
Yes Dear. the path is fine
Mendor81 26-Nov-12 11:22am    
Have you tried to close the Memorystream after saving, if you scheduling a save every 5 min for example and the MS is not closed and reopend again it won't save.. if that's not the case then sorry i'm out of ideas
Kasun Koswattha 26-Nov-12 11:31am    
i have closed the MS. but no luck. :(

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