Click here to Skip to main content
15,798,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i store an image in sql server file(sdf file) by changing it to an array of bytes,
then when i want to retrive the image and show it in the picture box, i use this method:
public static Image CreateImage(byte[] imageData)
          Image image=null;
          if(imageData !=null)
              using (MemoryStream inStream = new MemoryStream())
                    inStream.Write(imageData, 0, imageData.Length);

                    image = Bitmap.FromStream(inStream);

          return image;

but when i want to save the image retrived from database on the disk with this code:
if (pictueBox1.Image != null)
                pictueBox1.Image.Save(@"pic3.jpg", ImageFormat.Jpeg);

it gives an error :
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.

also this error doesn't happen when i load image from disk instead of database
Updated 27-Nov-12 1:07am
Mendor81 27-Nov-12 5:52am    
If the images displays well in the picturebox, then the save is failing, that could have several reasons.

- Make sure the path is correct.
- Make sure the file your trying to save doesn't exist yet or save with overwrite privilege
- Make sure the application has writing access to the path you've selected.
Reza Oruji 27-Nov-12 5:56am    
thanks,but it gives that error and when i go to that directory a file with 0kb is created
Mohd. Mukhtar 27-Nov-12 5:56am    
try to save with Server.MapPath
Reza Oruji 27-Nov-12 6:00am    
what is it? my database is local
sorry i am a newbie

1 solution

Saving the file requires that your app has write permission on the current folder - the folder containing the current web page. Set up a separate folder to hold them, and give write permission to all users.

If you don't have write permission, then GDI+ will give an error.
Share this answer
Reza Oruji 27-Nov-12 6:21am    
thank you,but it doesn't give an error when i load image into picturebox from disk instead of database
Mendor81 27-Nov-12 6:24am    
Have you tried giving another format than jpg on the save?
Reza Oruji 27-Nov-12 6:35am    
yes, and still same error

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