2013-07-04 75 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

相關問題