尝试检索图像时不显示图片

本文关键字:显示图 图像 检索 | 更新日期: 2023-09-27 18:36:56

我在数据库中的图像没有显示在窗口手机上。我只能在列表中获得 EmpNo,但图像没有显示在我的列表框中。我的编码有问题吗?

我在 MainPage.xaml 的编码是

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.Resources>
            <src:ImageConverter x:Key="imgConverter"/>
        </Grid.Resources>
        <ListBox Height="650" HorizontalAlignment="Left" Margin="11,17,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding EmpNo}"/>
                        <Image Grid.Column="1" Grid.Row="4" Height="136" HorizontalAlignment="Left"
               Margin="16,16,0,0" Name="imgEmp" Stretch="Fill"
               VerticalAlignment="Top" Width="200"
                Source="{Binding EmpImage}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
 </Grid>

我在主页.xaml.cs上的编码是

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        Service1Client svc = new Service1Client();
        svc.GetAllModelsCompleted += new EventHandler<GetAllModelsCompletedEventArgs>(svc_GetAllModelsCompleted);
        svc.GetAllModelsAsync();
    }
    void svc_GetAllModelsCompleted(object sender, GetAllModelsCompletedEventArgs e)
    {
        listBox1.ItemsSource = e.Result;
    }
}
public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var memStream = new MemoryStream((byte[])value);
        memStream.Seek(0, SeekOrigin.Begin);
        var empImage = new BitmapImage();
        empImage.CreateOptions = BitmapCreateOptions.None;
        empImage.SetSource(memStream);
        return empImage;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
  }

尝试检索图像时不显示图片

尝试应用转换器:

Source="{Binding EmpImage, Converter={StaticResource imgConverter}}"