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

时间:2022-06-18 16:33:37

The image is meant to be added to an update sql statement to update the image id a user changes the picture. I am trying to call the displayPhoto method into the savebtn method and assign it to residentImage variable as the image placeholder.

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

PhotoDisplay Method:

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

Save Button Method:

保存按钮方法:

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 个解决方案

#1


5  

You have defined your DisplayPhoto function to be a 'void' function, meaning that it returns nothing.
Then how do you expect to get something back in hello.Photo = DisplayPhoto(hello.Photo);?

您已将DisplayPhoto函数定义为“void”函数,这意味着它不返回任何内容。那你怎么期望在hello中得到回报.Photo = DisplayPhoto(hello.Photo);?

If you want to read something back from DisplayPhoto, probably you need to write something like this

如果你想从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
      }
}

#2


0  

You are trying to set hello.Photo to the return value of DisplayPhoto yet it's return type is void, e.g. it returns nothing at all. Problematic line is here:

您正在尝试将hello.Photo设置为DisplayPhoto的返回值,但它的返回类型为void,例如它根本不返回任何东西。有问题的路线在这里:

hello.Photo = DisplayPhoto(hello.Photo);

hello.Photo = DisplayPhoto(hello.Photo);

You need to make DisplayPhoto return a byte[]. Crude example: -

您需要使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];
}

#1


5  

You have defined your DisplayPhoto function to be a 'void' function, meaning that it returns nothing.
Then how do you expect to get something back in hello.Photo = DisplayPhoto(hello.Photo);?

您已将DisplayPhoto函数定义为“void”函数,这意味着它不返回任何内容。那你怎么期望在hello中得到回报.Photo = DisplayPhoto(hello.Photo);?

If you want to read something back from DisplayPhoto, probably you need to write something like this

如果你想从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
      }
}

#2


0  

You are trying to set hello.Photo to the return value of DisplayPhoto yet it's return type is void, e.g. it returns nothing at all. Problematic line is here:

您正在尝试将hello.Photo设置为DisplayPhoto的返回值,但它的返回类型为void,例如它根本不返回任何东西。有问题的路线在这里:

hello.Photo = DisplayPhoto(hello.Photo);

hello.Photo = DisplayPhoto(hello.Photo);

You need to make DisplayPhoto return a byte[]. Crude example: -

您需要使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];
}