Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9257407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:09:41+00:00 2026-06-18T12:09:41+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:09:42+00:00Added an answer on June 18, 2026 at 12:09 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now i am displaying the data from XML file in to grid view
I have a problem right now with CodeIgniter : I use the REST Controller
I have encountered a problem twice now whereby a producer thread produces N work
Hi I have problem with html height. My height is now set to pixels
I have a dynamic list code that works just fine. My only problem now
I have a problem that I'm working on for quite some time now. I
I'm onto problem 245 now but have hit some problems. I've done some work
Every now and then I have a problem with SVN inside eclipse folder gets
I have come across quite a wired problem right now. I am using ASP.NET
I have a problem here. Right now I'm doing my Web Based Application, my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.