-4

该图像旨在添加到更新 sql 语句中,以更新用户更改图片的图像 ID。我试图将方法调用displayPhotosavebtn方法中并将其分配给residentImage变量作为图像占位符。

照片显示方法:

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);
}
4

2 回答 2

5

您已将DisplayPhoto函数定义为“无效”函数,这意味着它不返回任何内容。
那么你如何期望得到一些东西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
      }
}
于 2013-07-04T13:30:57.677 回答
0

您正在尝试将其设置hello.Photo为的返回值,DisplayPhoto但它的返回类型是void,例如它什么也不返回。有问题的线路在这里:

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];
}
于 2013-07-04T13:41:53.273 回答