Click here to Skip to main content
12,951,983 members (61,884 online)
Rate this:
Please Sign up or sign in to vote.
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.

Posted 26-Nov-12 4:59am
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?
Razor_K 26-Nov-12 11:10am

yes. If i debug it is working fine. no exception. this is happening only in production server.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Razor_K 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:
Razor_K 26-Nov-12 11:19am
Dear, i have given the full permission to the local users. but still no luck. :(
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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);
Razor_K 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?
Razor_K 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
Razor_K 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100