문제

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