2015-01-28 5 views
1

SQL Server 데이터베이스의 일부 이미지를 ListBox에 표시하고 싶습니다.BitmapImage를 올바르게 표시하는 방법은 무엇입니까?

foreach (var screenshot in screenshots) 
{ 
    ImageListBox.Items.Add(screenshot); 
} 

출력은 다음과 같습니다 :

Output

가 어떻게 올바르게 이미지 (들)을 표시 할 수 있습니다 나는이 같은 추가 BitmapImage와 wenn 바이너리에서 이미지를 변환?

편집 : 이것은 내 XAML 코드 :

<ListBox Name="ImageListBox"> 
</ListBox> 

편집 2 :

이 나는 ​​이미지로 변환하는 방법입니다

public BitmapImage ConvertImage(byte[] value) 
{ 
    if (value != null && value.Length > 0) 
    { 
     using (MemoryStream stream = new MemoryStream(value)) 
     { 
      BitmapImage image = new BitmapImage(); 
      image.BeginInit(); 
      image.StreamSource = stream; 
      image.EndInit(); 
      return image; 
     } 
    } 
    return null; 
} 

그리고 이것은 내 쿼리입니다 :

var screenshots = context 
    .Error.Include(_ => _.ErrorScreenshots) 
    .First(_ => _.Id == selectedError.Id) 
    .Error 
    .Select(_ => new { Image = ConvertImage(_.Screenshot) }) 
    .ToArray(); 
+1

읽기 (http://wpftutorial.net/DataTemplates.html) (https://msdn.microsoft.com/en-us/library/ms742521.aspx ([MSDN] 참조)). – Sinatr

+0

ListBox가 기본적으로 toString()을 호출하고 그것이 이상하게 보이는 이유는 정확하다는 것을 알고 있습니까? –

답변

2

ListBox는 그것은 toString() 메서드를 사용하여 객체를 표시하는 것입니다. 귀하의 경우에는 "Image = System.Windows.Media.Bitmap.Image"로 끝납니다.

각 항목을 Image 개체로 표시해야 함을 나타 내기 위해 ItemTemplate을 지정하십시오. [데이터 템플릿 내지 약

<ListBox Name="ImageListBox"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal" > 
     <Image Source="{Binding}" /> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

소스에 몇 가지 문제가 있습니다. 이미지는 데이터베이스에서 가져온 것이므로 어떤 소스를 지정해야하는지 알지 못합니다. –

+1

답변을 업데이트했습니다. 나는 당신의 경우 BitmapImage 객체를 사용하고 있기 때문에 ImageSource에 바인딩하는 대신 객체에 직접 바인딩 할 수 있다고 생각합니다. BitmapImage는 작동해야하는 ImageSource입니다. –

+0

이미지가 비어 있거나 null 일 수 있습니까? [link] (https://www.dropbox.com/s/xj371j2qc6jwyon/ImageIsNull.png?dl=0) –

관련 문제