字节[]到位图图像 [英] Byte [] to Bitmap Image
问题描述
大家好
我是WP新手。我对图像有疑问。我有一个从数据库填充的列表框。此外,还有一个图像列来自SQL中的主表。然后,应用程序在列表框中显示这些列。除了图像之外,所有列都来自数据库
。图像从数据库填充为byte []。这就是为什么,它无法在应用程序中显示。我必须将所有byte [] s转换为bitmapimages。我的代码如下。请看一看。提前致谢。
I am new in WP. I have a question regarding to Images. I have a listbox which populates from database. Also, there is a image column which comes from main table in SQL. Then, the app displays these columns in a listbox. All the columns are coming from database good except images. Image is populated from database as byte[]. That's why, it cannot be displayed in the app. I have to convert all byte[] s to bitmapimages. My code is below. Please have a look. Thanks in advance.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Name="lstbox1" Margin="0,0,0,0" Tag="{Binding ID}" SelectedValuePath="{Binding Path=ID}" SelectionChanged="lstbox1_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem x:Name="btn1" Width="460" BorderThickness="2" Height="Auto" BorderBrush="Black">
<ListBoxItem.Content>
<StackPanel Orientation="Horizontal" Height="80" Width="400">
<Image Source="{Binding Image}" Width="80" Height="Auto"/>
<StackPanel Orientation="Horizontal" Height="Auto">
<TextBlock Width="Auto" FontSize="22" Text="{Binding Header}" Height="Auto"/>
</StackPanel>
</StackPanel>
</ListBoxItem.Content>
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
public void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
StudentServiceClient serviceClient = new StudentServiceClient();
serviceClient.GetAllStudentsCompleted += new EventHandler<GetAllStudentsCompletedEventArgs>(serviceClient_GetAllStudentsCompleted);
serviceClient.GetAllStudentsAsync();
}
void serviceClient_GetAllStudentsCompleted(object sender, GetAllStudentsCompletedEventArgs e)
{
if (e.Result != null)
{
lstbox1.ItemsSource = e.Result;
}
}
推荐答案
你的照片是什么格式?它们是JPEG编码的吗?
What format are your pictures in? Are they JPEG encoded?
通常,您可以从byte [] - Array构造一个MemoryStream,并从该Stream加载Image。为了使用DataBinding,您必须将代码放在进行转换的ValueConverter中。
In general you can construct a MemoryStream from a byte[]-Array and load the Image from that Stream. In order to make that work with DataBinding you will have to put the code inside a ValueConverter that does the conversion.
这篇关于字节[]到位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!