【问题标题】:Cannot implicitly convert type 'void' to 'byte[]'无法将类型“void”隐式转换为“byte[]”
【发布时间】:2013-07-04 13:28:39
【问题描述】:

该图像旨在添加到更新 sql 语句中以更新用户更改图片的图像 ID。我正在尝试将displayPhoto 方法调用到savebtn 方法中,并将其分配给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);
}

【问题讨论】:

  • 请注明在哪一行。
  • 这是因为你有 void 类型的东西,并将它分配给需要 byte[] 类型的东西
  • 请尽量保持您的代码整洁,易于阅读。

标签: c# .net sql wpf


【解决方案1】:

您已将 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
      }
}

【讨论】:

  • 我如何从 DisplayPhoto 返回一些东西?
  • @user2122032 你真的应该阅读基础知识。它将为您节省很多的麻烦和时间。我不是在开玩笑。
  • 我不能在 if 语句之外返回
  • @user2122032 你绝对可以
  • @user2122032 所有代码路径都需要返回值,因此如果您在if 内使用return,您还必须在属于elseelse 内使用return 或条件外。或者,您可以创建一个byte[] 变量,将您需要的任何内容分配给它,然后在方法的最后将return 分配给它。
【解决方案2】:

您试图将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];
}

【讨论】:

  • @user2122032 为什么不呢?你期待什么,它会做什么?
  • Stills 给了我错误:无法将类型 'byte[]' 隐式转换为 'System.Windows.Controls.Image'
猜你喜欢
  • 2021-12-02
  • 2013-02-22
  • 1970-01-01
  • 2014-04-14
  • 2016-02-17
  • 2015-04-26
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多