从数据库中检索字节数组图像并在图片框控件中显示时出现问题
Trouble retrieving byte array image from database and display in picture box control
我在将字节数组的图像从数据库检索到 WPF 页面的图片框中时遇到了一些问题。这是我从字节转换为图像的代码:
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;
}
我在从数据库读取数据的 SQlDataReader
中调用此方法时遇到问题。这是下面的代码:
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");
}
我很迷茫,我不知道我做错了什么,我只是收到这么多不同类型的异常。请大神帮我查一下代码,指正一下。
行
imgprofilePicture.DataContext =
(Image)studentReader[GetBitmapImageFromBytes(ImageArray)];
没有意义。
不应有 PictureBox,而是 XAML 中的 Image
元素,例如喜欢
<Image x:Name="image" .../>
然后您可以将图像的 Source
属性 设置为从您的方法返回的 BitmapImage:
image.Source = GetBitmapImageFromBytes((byte[])studentReader["ImageArray"]);
我在将字节数组的图像从数据库检索到 WPF 页面的图片框中时遇到了一些问题。这是我从字节转换为图像的代码:
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;
}
我在从数据库读取数据的 SQlDataReader
中调用此方法时遇到问题。这是下面的代码:
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");
}
我很迷茫,我不知道我做错了什么,我只是收到这么多不同类型的异常。请大神帮我查一下代码,指正一下。
行
imgprofilePicture.DataContext =
(Image)studentReader[GetBitmapImageFromBytes(ImageArray)];
没有意义。
不应有 PictureBox,而是 XAML 中的 Image
元素,例如喜欢
<Image x:Name="image" .../>
然后您可以将图像的 Source
属性 设置为从您的方法返回的 BitmapImage:
image.Source = GetBitmapImageFromBytes((byte[])studentReader["ImageArray"]);