Search code examples
c#imagecompact-frameworkwindows-ceinvoke

pictureBox.InvokeRequired not showing img


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.


Solution

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