无法隐式转换类型' void'到' byte []'

时间:2013-07-04 13:28:39

标签: c# .net sql wpf

该图像旨在添加到更新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);
}

2 个答案:

答案 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];
}
相关问题