该图像旨在添加到更新sql语句中,以更新用户更改图片的图像ID。我尝试将displayPhoto
方法调用savebtn
方法,并将其作为图像占位符分配给residentImage
变量。
PhotoDisplay方法:
private void DisplayPhoto(byte[] photo)
{
if (!(photo == null))
{
MemoryStream stream = new MemoryStream();
stream.Write(photo, 0, photo.Length);
stream.Position = 0;
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Seek(0, SeekOrigin.Begin);
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
ResidentImage.Source = bitmapImage;
ResidentProfileImage.Source = bitmapImage;
}
else
{
ResidentImage.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/ResidentImage.jpg"));
}
}
保存按钮方法:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
Resident hello = new Resident();
hello.Doctor = new Doctor();
hello.Room = new Room();
hello.addtionalInformation = txtAdditionalInformation.Text;
hello.FirstName = txtForename.Text;
hello.Surname = txtSurname.Text;
hello.Title = txtTitle.Text;
hello.ResidentID = GlobalVariables.SelectedResident.ResidentID;
hello.Doctor.DoctorID = GlobalVariables.SelectedResident.Doctor.DoctorID;
hello.Room.RoomID= GlobalVariables.SelectedResident.Room.RoomID;
hello.Room.Name = txtRoomNo.Text;
hello.Allergies = txtResidentAlergies.Text;
hello.Photo = DisplayPhoto(hello.Photo);
hello.DateOfBirth = DateTime.Parse(txtDOB.Text);
ResidentData.Update(hello);
}
答案 0 :(得分:5)
您已将DisplayPhoto
函数定义为'void'函数,这意味着它不返回任何内容
那你怎么期望在hello.Photo = DisplayPhoto(hello.Photo);
得到回报?
如果你想从DisplayPhoto读回来的东西,可能你需要写这样的东西
public byte[] DisplayPhoto(byte[] photo)
{
if (!(photo == null))
{
MemoryStream stream = new MemoryStream();
// .......
return stream.ToArray();
}
else
{
// Convert your existing ResidentImage.Source to a byte array and return it
}
}
答案 1 :(得分:0)
您正尝试将hello.Photo
设置为DisplayPhoto
的返回值,但它的返回类型为void
,例如它根本不返回 nothing 。有问题的一行在这里:
hello.Photo = DisplayPhoto(hello.Photo);
您需要DisplayPhoto
返回byte[]
。粗俗的例子: -
private byte[] DisplayPhoto(byte[] photo)
{
if (!(photo == null))
{
MemoryStream stream = new MemoryStream();
stream.Write(photo, 0, photo.Length);
stream.Position = 0;
System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Seek(0, SeekOrigin.Begin);
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();
ResidentImage.Source = bitmapImage;
ResidentProfileImage.Source = bitmapImage;
return stream.ToArray();
}
else
{
ResidentImage.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/ResidentImage.jpg"));
}
return new byte[0];
}