2013-07-04 45 views
-4

该图像旨在添加到更新sql语句中以更新用户更改图片的图像ID。我试图将displayPhoto方法调用为savebtn方法,并将其分配给residentImage变量作为图像占位符。无法将类型'void'隐式转换为'byte []'

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); 
} 
+1

请指出在哪条线上。 –

+2

这是因为你有一些类型为'void'的东西,并将它分配给需要类型为'byte []'的东西' – Matthew

+0

请尽量保持代码的整洁,并且易于阅读。 – Sam

回答

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 
     } 
} 
+0

如何从DisplayPhoto返回一些东西? – user2122032

+2

@ user2122032你真的***应该阅读有关基础知识。它会为你节省很多麻烦和时间。我不是在开玩笑。 – Nolonar

+0

我不能在if语句之外返回 – user2122032

0

您正在尝试设置hello.PhotoDisplayPhoto返回值但它的返回类型为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]; 
} 
+0

仍然没有工作 – user2122032

+0

@ user2122032为什么不呢?你在期待什么,它是做什么的? – Nolonar

+0

剧照会给我错误:\t无法将类型'byte []'隐式转换为'System.Windows.Controls.Image' – user2122032

相关问题