Click here to Skip to main content
12,621,953 members (35,213 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# GDI+
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
Posted 27-Nov-12 0:46am
Updated 27-Nov-12 1:07am
v2
Comments
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2016
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