我在SQLite数据库中存储了图像字节。当我想获得单个图像然后我通过id得到它并将图像字节转换为位图图像。但是当我想将数据列表绑定到listBox时,我无法将图像控制直接绑定到字节。
我的Xaml代码在这里。
<ListBox Background="Transparent" Margin="-5,2,-5,10" BorderThickness="0" MaxHeight="680" Grid.Row="1" x:Name="listBoxobj" SelectionChanged="listBoxobj_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="400" Margin="0,-10,0,0" >
<Grid x:Name="pro_work_grid" Width="400" Height="120" Margin="0,0,0,0"
VerticalAlignment="Bottom" Background="#31b1b0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
//Image will give here image bytes. not the source.
// Not know how to bind this with list,,which contain image bytes
<Image x:Name="work_pic" Source="{Binding image}" Width="120" Height="120"
Stretch="Fill" VerticalAlignment="Center" Margin="0,0,0,0"
Grid.ColumnSpan="1" HorizontalAlignment="Left"/>
<TextBlock x:Name="pro_name1" Foreground="White" VerticalAlignment="Top" Grid.ColumnSpan="2" Margin="130,10,0,0"
Text="{Binding category}" FontSize="26" FontWeight="Bold">
</TextBlock>
<TextBlock x:Name="pro_title1" VerticalAlignment="Center" Grid.ColumnSpan="2" Margin="130,10,0,0"
Text="{Binding title}" FontSize="20" Foreground="White">
</TextBlock>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我将文本块直接按类别绑定,而标题..则列在列表中。
当我的ListBox页面加载然后代码工作时,
private void ReadContactList_Loaded(object sender, RoutedEventArgs e)
{
ReadAllContactsList dbcontacts = new ReadAllContactsList();
DB_ContactList = dbcontacts.GetAllContacts();//Get all DB contacts
if (DB_ContactList.Count > 0)
{
}
listBoxobj.ItemsSource = DB_ContactList.OrderByDescending(i => i.Id).ToList();
}
我能够从单个图像字节中获取图像。但这里是图像列表显示,,,那么如何获取字节并将它们转换为图像控制并在图像控制中单独显示它们。 这是我的列表框项目
你能帮帮我吗?我不知道正确的语法,,,先谢谢我以这种方式将图像转换为字节。
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
StorageFile storageFile = args.Files[0];
string pro_img_path = storageFile.Path;
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
filebite = new byte[stream.Size];
using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(filebite);
}
再次将这些字节重新转换为位图图像。
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])pro_pic_byts);
writer.StoreAsync().GetResults();
}
var img1 = new BitmapImage();
img1.SetSource(ms);// = ms;
pro_pic.Source = img1;
}
答案 0 :(得分:0)
根据您存储图像字节的方式,这可能有效:
public class BinaryToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return null;
if (value is IEnumerable<byte>)
{
List<byte> imgData = new List<byte>((IEnumerable<byte>)value);
BitmapImage rval = new BitmapImage();
rval.SetSource(new System.IO.MemoryStream(imgData.ToArray()));
return rval;
}
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
将ImageSource绑定到byte[]
答案 1 :(得分:0)
刚刚将位代码添加到数据库类,然后绑定到bitmapImage
<Image Grid.RowSpan="4" HorizontalAlignment="Right" VerticalAlignment="Top" Source="{Binding _contact.bitmapImage}" Stretch="UniformToFill" Margin="5" Width="100" Height="100" Opacity="1" />
public byte[] Image
{
get
{
return (_imgBytes);
}
set
{
_imgBytes = value;
CreateBitmap();
}
}
private async void CreateBitmap()
{
var result = await dbUtils.CreateBitmap(_imgBytes);
_bitmap = result;
}
[Ignore]
public BitmapImage bitmapImage
{
get
{
return (_bitmap);
}
}