与SQLite绑定的列表框图像控件,图像字节存储在数据库C#中 [英] Listbox image control binding with SQLite , Image bytes are stored in Database C#

查看:77
本文介绍了与SQLite绑定的列表框图像控件,图像字节存储在数据库C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将图像字节存储在SQLite数据库中.当我想获取单个图像时,我通过id将该图像转换为位图图像.但是,当我想将数据列表绑定到listBox时,则无法将图像控件直接绑定到字节.

I have stored image bytes in SQLite database . when I want to get single image then I got that by id and convert image bytes to bitmap image. But when i want to bind list of data to listBox then I'm not able to bind directly image control to bytes.

我的Xaml代码在这里.

My Xaml code is here.

  <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> 

我已经将文本块直接绑定到列表中的类别和标题.

I have bind text block to directly by category,and title.. which are coming in List.

当我的ListBox页面加载后,代码便工作了,

when my ListBox page loads then code work,,

  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();
    }

我能够从单个图像字节中获取图像.但是这里是图像显示的列表,因此,如何获取字节并将其转换为图像控件,并在图像控件中分别显示它们. 这是我的列表框项目

I'm able to get image from single image bytes. but here is the list of images display,,, so how can I get bytes and convert them into image control and show them separately in image control. Here is my Listbox items

请你帮帮我吗?我不知道正确的语法谢谢"

Would you please help me?? I don't know the proper Syntax,,,Thanks in Advance

我以这种方式将图像转换为字节.

I convert image to bytes in this way.

   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);
            }

再次将这些字节转换为位图图像.

Again to reconvert these bytes to bitmap image.

   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;
        } 

推荐答案

只是将位代码添加到数据库类中,然后绑定到bitmapImage

just added bit code to database class and then bind to 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);
        }
    }

这篇关于与SQLite绑定的列表框图像控件,图像字节存储在数据库C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆