public EmployeeProfile(int EmpID)
{
      if (byteArrayToImage(Emp.Photo.ToArray()) != null)
            {
                pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
                pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
            }
}
public Image byteArrayToImage(Byte[] byteArrayIn)
{
       MemoryStream ms = new MemoryStream(byteArrayIn);
       Image returnImage = Image.FromStream(ms);
       return returnImage;
}

I have this code when an employee doesn't have a picture a message will appear:enter image description here

有帮助吗?

解决方案

It looks like Emp.Photo is null and calling ToArray() is giving you the null reference exception. Try this:

if (Emp.Photo != null)
{
    pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
    pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
}

其他提示

Seems that your Emp originally doesn't have a Photo, which means the Emp.Photo is null, that's why you're having the NullReferenceException whose message is "Object reference not set to an instance of an object", you need first to check if your Emp object has a Photo or not :

public EmployeeProfile(int EmpID)
        {
              if (Emp.Photo != null)
                    {
                        pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
                        pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
                    }
        }

Make sure the Emp and Photo are not null:

public EmployeeProfile(int EmpID)
{
      if (Emp != null && Emp.Photo != null)
      {
           pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
           pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
      }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top