0

Possible Duplicate:
GDI+ Generic Error

i am writing a db application and use "sdf" file as a database. i save the image on the DB by converting it to a byte array by this code :

byte[] ReadFile(string sPath)
    {

        //Initialize byte array with a null value initially.
        byte[] data = null;

        //Use FileInfo object to get file size.
        FileInfo fInfo = new FileInfo(sPath);
        long numBytes = fInfo.Length;

        //Open FileStream to read file
        FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

        //Use BinaryReader to read file stream into byte array.
        BinaryReader br = new BinaryReader(fStream);

        //When you use BinaryReader, you need to supply number of bytes to read from file.
        //In this case we want to read entire file. So supplying total number of bytes.
        data = br.ReadBytes((int)numBytes);
        return data;
    }

and insert it into database.

i retrive the image via this code:

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;
    }

and assign the pictureBox Image to CreateImage return value.

until here every thing is fine but when i want to store pictureBox image on the disk

 Bpicture.Image.Save(@"pic1.jpeg", ImageFormat.Jpeg);

this error occurs :

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

Community
  • 1
  • 1
ReZa
  • 1,273
  • 2
  • 18
  • 33
  • diff the byte array from db, and from disk. Are they exactly same? – Tilak Nov 27 '12 at 13:40
  • You are very sloppy about Close and Dispose calls. That inevitably goes wrong when your program still has the file opened and you try to save back to the same file. – Hans Passant Nov 27 '12 at 14:13
  • @ Hans Passant can you show me a code? i didn't get it – ReZa Nov 27 '12 at 14:26
  • Hans is pointing that you should call methods Close nad Dispose on file stream and Dispose on Bitmap instances when you are done with them. – Rafal Nov 27 '12 at 14:32
  • @Rafal do you mean in my methods(ReadFile and CreateImage)? can you please edit them? – ReZa Nov 27 '12 at 14:42
  • @RezaOruji - Check out the accepted answer from my link to a duplicate question above. Your problem may be that you are destroying the memory stream prematurely. – mbeckish Nov 27 '12 at 14:44

1 Answers1

0

Try to modify your code:

inStream.Write(imageData, 0, imageData.Length);
inStream.Seek(0, SeekOrigin.Begin);
image = Bitmap.FromStream(inStream);

stream is a specific structure that has a pointer that needs to be moved to beginning in order to read the array that was just written to it.

Rafal
  • 12,391
  • 32
  • 54
  • Thank you, but still same error – ReZa Nov 27 '12 at 13:57
  • are you certain that bytes represent valid picture? Why wont you read bitmap from `fstream` directly? (You can simplify your `ReadFile` by [this method](http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx)) – Rafal Nov 27 '12 at 14:06
  • used File.ReadFile method instead of my own method,but still same error – ReZa Nov 27 '12 at 14:25