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 8324469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:05:04+00:00 2026-06-09T00:05:04+00:00

I’m receiving from socket communication an image as byte[] and then I try to

  • 0

I’m receiving from socket communication an image as byte[] and then I try to show it in a pictureBox. When I run the code it shows a message error only saying: "NullReferenceException"

The catch handling the exception is ex1 and I checked and the pic isn’t null so I can’t figure it out why this exception is happening.

This is my code:

try
{
    if (pictureBox1.InvokeRequired)
    {
        try
        {
            pic = imageEmp;
            addControlHandler c = new addControlHandler(addPic);
            this.Invoke(c);
        }
        catch (Exception exc) { MessageBox.Show(exc.Message); }
    }
    else
    {
        pictureBox1.Image = ByteToImage(imageEmp);
    }
}
catch (Exception ex1) 
{
    MessageBox.Show(ex1.Message);                
}

public void addPic()  //when invokeRequired == true
{
    pictureBox1.Image = ByteToImage(pic); 
}

Here is the code to convert byte[] to Image:

public Image ByteToImage(byte[] imageBytes)  //convert byte[] to image
{
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = new Bitmap(ms); 
    return image;
}

UPDATE 1: Regarding Hans answer, I make he following changes:

Change my ByteToImage to Hans’s answer and to check where the error is I added this lines in the place where this.Invoke(c) was:

if (c != null)
{
    try
    {
        this.Invoke(c);
    }
    catch (Exception e_c)
    {
        MessageBox.Show(e_c.Message, "exc from e_c");
    }
}

This give me an exception : NullReferenceException

Thanks for any help!

UPDATE 2: Now it’s working, I send JPEG images instead of JPG and it show it now. Don’t know why this happens but now it’s working ok.

  • 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-09T00:05:07+00:00Added an answer on June 9, 2026 at 12:05 am

    Here is an example that you can try I have tested this just now using my own method this works so replace your code with the lines of code in my btnStudentPic_Click let me know if this works for you..

    For Compact Framework try this instead

    public static byte[] ReadAllBytes(string path)
    {
    byte[] buffer;
    
    using (FileStream fs = new FileStream(path, FileMode.Open,
    FileAccess.Read, FileShare.Read))
    {
    int offset = 0;
    int count = (int)fs.Length;
    buffer = new byte[count];
    while (count > 0)
    {
    int bytesRead = fs.Read(buffer, offset, count);
    offset += bytesRead;
    count -= bytesRead;
    }
    }
    
    return buffer;
    }
    

    //Windows example below don’t use for CF

        private void btnStudentPic_Click(object sender, EventArgs e)
        {
            Image picture = (Image)BrowseForPicture();
            this.picStudent.Image = picture;
            this.picStudent.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Bitmap BrowseForPicture()
        {
           // Bitmap picture = null;
    
            try
            {
                if (this.fdlgStudentPic.ShowDialog() == DialogResult.OK)
                {
                    byte[] imageBytes = File.ReadAllBytes(this.fdlgStudentPic.FileName);
                    StudentPic = new Bitmap( this.fdlgStudentPic.FileName);
                    StuInfo.StudentPic = imageBytes;
                }
                else
                {
                    StudentPic = Properties.Resources.NoPhotoAvailable;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("That was not a picture.", "Browse for picture");
                StudentPic = this.BrowseForPicture();
            }
    
            return StudentPic;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.