lolo
lolo

Reputation: 39

what is the meaning of this error "out of memory" when using picturebox1.Image equal to image FromFile(fileName)?

I have a problem now with displaying an image from an exist file ...

try
{   
    bool EndFlag = false;
    string fileLoc = @"../../../../samples/jpeg_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".jpg";
    //Create a file Stream to save the body of the JPEG File content.
    FileStream fs = null;
    fs = new FileStream(fileLoc, FileMode.OpenOrCreate, FileAccess.Write);

    do 
    {
        ReadJpegFileCommand(); 
        CamPort.DiscardOutBuffer();
        CamPort.DiscardInBuffer();

        for (int i = 0; i < 5; i++)
            header[i] = (byte)CamPort.ReadByte();

         if (((int)header[0] == 0x76) && (header[1] == 0x00) && (header[2] == 0x32) && (header[3] == 0x00) && (header[4] == 0x00))
             {
                for (int i = 0; i < 32; i++)
                        ImageBody[i] = (byte)CamPort.ReadByte();
              /* 
                 * writing the bytes that have been read till now to a file 
              */                    

                    fs.Write(ImageBody, 0, ImageBody.Length);

                    for (int i = 1; i < ImageBody.Length; i++)     // check if reached to the last two bytes(FF D9) of the body to  stop reading the body.
                    {
                        if ((ImageBody[i - 1] == 0xFF) && (ImageBody[i - 0] == 0xD9))
                        {
                            EndFlag = true;
                            MessageBox.Show("FFD9 has been received");
                            OneSnap.Image =(Bitmap)System.Drawing.Image.FromStream(fs);
                            fs.Close();

                        }
                    }
             }
         else
         {
             MessageBox.Show("Error,Try again"); // The first 5 bytes does not match the header 

         }


         for (int i = 0; i < footer.Length; i++)
         {
             footer[i] = (byte)CamPort.ReadByte();
         }



         // update the starting address 

         M += (UInt16)ImageBody.Length;
         //Progress.PerformStep();


        }while(!EndFlag);


    }



catch (System.Exception ex) { MessageBox.Show(ex.Message); }

When I have used this statements :

OneSnap.Image =(Bitmap)System.Drawing.Image.FromStream(fs);
                                                            fs.Close();

I had this error : "Parameter is not valid"

but when I've tried with alternative way and replaced the previous statements by ::

 fs.Close();
OneSnap.Image =(Bitmap)System.Drawing.Image.FromFile(fileLoc);

I showed the image in the picture box .. but then when I have executed the program more I had this error :: " Out of memory " and couldn't see the image in the picture box (OneSnap) >>> How to solve this ??

sample :: ( this image has been captured by link Sprite Jpeg Camera )

enter image description here

Upvotes: 0

Views: 215

Answers (1)

Shadow Wizard
Shadow Wizard

Reputation: 66389

Looks like the file you created is not a valid picture, so can't be converted to a Bitmap.

See the official docuemtation:

Exception             Condition
-------------------------------
OutOfMemoryException  The file does not have a valid image format.
                      -or-
                      GDI+ does not support the pixel format of the file.

Can't see a way to "fix" this, but you can verify by trying to view the file in picture viewer; if you can view it then you might need something more complex than what System.Drawing offers.

Edit: might be easier than any of us imagined. Try changing the order of your lines:

fs.Close();
OneSnap.Image =(Bitmap)System.Drawing.Image.FromStream(fs);

Might be that while the stream is open, the Bitmap internal code can't read from the file.

Another approach is using MemoryStream instead. For this, first add a List to store all the bytes:

List<byte> arrAllBytes = new List<byte>();

Now instead of this line:

fs.Write(ImageBody, 0, ImageBody.Length);

Have this code:

arrAllBytes.AddRange(ImageBody);

And finally:

MemoryStream stream = new MemoryStream(arrAllBytes.ToArray())
OneSnap.Image = System.Drawing.Image.FromStream(stream);

Upvotes: 2

Related Questions