Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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)
        {
            try
            {
 
                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);
                    returnImage.Dispose();
 
                }
            }
            catch (Exception ex)
            {
                UpdateEmailStatus("Sending Failed-byteArrayToImage3" + " " + ex.Message);
            }
            return _fullImagePath;
 
        }
please help me on this.
I have a production release tomorrow.
 
thanks.
Posted 26-Nov-12 4:59am
Razor_K469
Edited 26-Nov-12 5:18am
v2
Comments
Mendor81 at 26-Nov-12 11:08am
   
Have you tried a step by step debug? at which line does it throw the exception?
Razor_K at 26-Nov-12 11:10am
   
Hi,
 
yes. If i debug it is working fine. no exception. this is happening only in production server.
Rate this: bad
good
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?
 
[edit]
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.
[/edit]
  Permalink  
v2
Comments
Razor_K at 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 at 26-Nov-12 11:14am
   
See edited answer - I saw the "scheduled" bit just as I pressed the "post message" button! :blush:
Razor_K at 26-Nov-12 11:19am
   
Dear, i have given the full permission to the local users. but still no luck. :(
Rate this: bad
good
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);
 
mss.Close();
fs.Close();
}
  Permalink  
Comments
Razor_K at 26-Nov-12 11:13am
   
but if i run it manually instead of scheduling, its working fine, the image is saving.
Mendor81 at 26-Nov-12 11:17am
   
Could be a dump question, but have you checked the path when scheduling the save?
Razor_K at 26-Nov-12 11:19am
   
Yes Dear. the path is fine
Mendor81 at 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 at 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
0 Sergey Alexandrovich Kryukov 403
1 OriginalGriff 354
2 George Jonsson 202
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
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