简体   繁体   English

无法从数据库中检索字节数组图像并在图片框控件中显示

[英]Trouble retrieving byte array image from database and display in picture box control

Please I am having some issues retrieving image of byte array from database into picture box in WPF page.请我在将字节数组的图像从数据库中检索到 WPF 页面中的图片框时遇到一些问题。 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.我在从数据库读取数据的SQlDataReader中调用此方法时遇到问题。 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.请有人帮我检查代码并纠正我。

The line线

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

makes no sense.没有意义。

There should not be a PictureBox, but an Image element in XAML, eg like XAML 中不应该有 PictureBox,而是Image元素,例如

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

You would then set the Image's Source property to the BitmapImage returned from your method:然后,您可以将 Image 的Source属性设置为从您的方法返回的 BitmapImage:

image.Source = GetBitmapImageFromBytes((byte[])studentReader["ImageArray"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM