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;
}
当员工没有图片时,我会显示此代码:
答案 0 :(得分:2)
看起来Emp.Photo
是null
,调用ToArray()
会给你空引用异常。试试这个:
if (Emp.Photo != null)
{
pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
}
答案 1 :(得分:0)
似乎您的Emp
最初没有照片,这意味着Emp.Photo为空,这就是为什么您有NullReferenceException消息的原因是" 对象引用未设置为对象的实例",您需要先检查您的Emp对象是否有照片:
public EmployeeProfile(int EmpID)
{
if (Emp.Photo != null)
{
pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
答案 2 :(得分:0)
确保 Emp 和照片不为空:
public EmployeeProfile(int EmpID)
{
if (Emp != null && Emp.Photo != null)
{
pictureBoxEmp.Image = byteArrayToImage(Emp.Photo.ToArray());
pictureBoxEmp.SizeMode = PictureBoxSizeMode.StretchImage;
}
}