-2

Please I am having some issues retrieving image of byte array from database into picture box in WPF page. This is my code to convert from byte to image:

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.Freeze();
    }
    return btm;
}

I am having trouble calling this method in the SQlDataReader where I am reading from the database. This is the code below:

    private void StudentRegistrationForm_Loaded(object sender, RoutedEventArgs e)
    {        
        try
        {
            StudentConn = new 
  SqlConnection(ConfigurationManager.ConnectionStrings["SchoolDB"].ConnectionString);
                StudentCmd = new SqlCommand();
                StudentCmd.Connection = StudentConn;
                StudentCmd.CommandType = CommandType.StoredProcedure;
                StudentCmd.CommandText = "spStudent_GetAll";

            StudentConn.Open();
            studentReader = StudentCmd.ExecuteReader();

                   if (studentReader.Read())
                   {
                        TxtbID.DataContext = (int)studentReader["Id"];
                        TxtLastName.DataContext = (string)studentReader["LastName"];
                        TxtFirstName.DataContext = (string)studentReader["FirstName"];
                        TxtEmail.DataContext = (string)studentReader["Email"];
                        CboGender.SelectedItem = (string)studentReader["Gender"];
                        DtpDateOfBirth.DataContext = (DateTime)studentReader["DateofBirth"];
                        DtpDateRegistered.DataContext = (DateTime)studentReader["DateRegistered"];
                        TxtPhone.DataContext = (string)studentReader["MobileNumber"];
                        TxtComment.DataContext = (string)studentReader["Notes"];
                        CboReligion.SelectedItem = (string)studentReader["Religion"];
                        imgprofilePicture.DataContext = (Image)studentReader[GetBitmapImageFromBytes(ImageArray)];
                        CboSpecialAttention.SelectedItem = (string)studentReader["SpecialAttention"];
                        TxtGuardianID.DataContext = (int)studentReader["GuardianID"];

                   }

        }

        catch (Exception ex)
            {
                if (ex is SqlException || ex is SystemException || ex is NullReferenceException)
                {

                MessageBox.Show(ex.Message, "Error Establishing Connection to Student Data Service", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        finally
            {
                StudentConn.Close();
                StudentConn.Dispose();
                studentReader.Close();
                StudentCmd.Dispose();               
            }
        SetState("View");
    }   

I am seriously lost, I don't know what I did wrong, I am just getting exceptions of so many different types. Please someone help me check the code and correct me.

Dale K
  • 25,246
  • 15
  • 42
  • 71
DaPlug
  • 9
  • 4
  • 1
    It looks totally odd to set the DataContext property of all those controls. Either you set their Text (or similar) properties directly, or you set the DataContext of their common parent element once to a data object that you create when you read data from the db, and bind the UI element properties to the properties of the data object. – Clemens Dec 15 '20 at 10:18

1 Answers1

0

The line

imgprofilePicture.DataContext =
    (Image)studentReader[GetBitmapImageFromBytes(ImageArray)];

makes no sense.

There should not be a PictureBox, but an Image element in XAML, e.g. like

<Image x:Name="image" .../>

You would then set the Image's Source property to the BitmapImage returned from your method:

image.Source = GetBitmapImageFromBytes((byte[])studentReader["ImageArray"]);
Clemens
  • 123,504
  • 12
  • 155
  • 268