I have a byte array to image converter method that converts image stored as byte array in SQL server database to image type for display in the image control. I am confuse on how to call the method to display the image. I am using mvvm pattern. Here is my student service class:
public List<Student> GetAll()
{
List<Student> StudentList = new List<Student>();
try
{
StudentCmd.Parameters.Clear();
StudentCmd.CommandText = "spStudent_GetAll";
StudentConn.Open();
var StudentReader = StudentCmd.ExecuteReader();
if (StudentReader.HasRows)
{
student = null;
while (StudentReader.Read())
{
student = new Student
{
Id = (int)StudentReader["Id"],
LastName = (string)StudentReader["LastName"],
FirstName = (string)StudentReader["FirstName"],
Email = (string)StudentReader["Email"],
Gender = (string)StudentReader["Gender"],
DateofBirth = (DateTime)StudentReader["DateofBirth"],
DateRegistered = (DateTime)StudentReader["DateRegistered"],
MobileNumber = (string)StudentReader["MobileNumber"],
Notes = (string)StudentReader["Notes"],
Religion = (string)StudentReader["Religion"],
Image = (byte[])StudentReader["Image"],
SpecialAtttention = (string)StudentReader["SpecialAttention"]
};
StudentList.Add(student);
}
}
StudentReader.Close();
}
catch (SqlException ex)
{
throw ex;
}
finally
{
StudentConn.Close();
}
return StudentList;
}
And this is my byte array to image converter method:
private BitmapImage GetBitmapImageFromBytes(byte[] bytes)
{
BitmapImage btm;
using (MemoryStream stream = new MemoryStream(bytes))
{
btm = new BitmapImage();
btm.BeginInit();
btm.StreamSource = stream;
btm.CacheOption = BitmapCacheOption.OnLoad;
btm.EndInit();
btm.Freezenter code here
e();
}
return btm;
}
This is my student model:
public class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int Id { get; set; }
public int GuradianID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public DateTime DateofBirth { get; set; }
/// <summary>
/// Date enrolled in school
/// </summary>
public DateTime DateRegistered { get; set; }
public string MobileNumber { get; set; }
/// <summary>
/// List of student addresses if he/she has more than one
/// </summary>
public List<Address> Addresses { get; set; } = new List<Address>();
public string Notes { get; set; }
public string Religion { get; set; }
public byte[] Image { get; set; }
/// <summary>
/// Determine student who needs extra lessons
/// </summary>
public string SpecialAtttention { get; set; }
How do I call this method to convert byte array to image for display to user?