locked
show image gridview with sqlite RRS feed

  • Question

  • hi people, 

    how show images in grid view with database sqlite, my table is that:

    public class Escola
            {
                [AutoIncrement, PrimaryKey]
                public int ID_Escola { get; set; }
                public string NomeEscola { get; set; }
                public byte[] Imagem { get; set; }
    
                
            }

    and my gridview in XAML: 

     <Image Margin="0" Source="{Binding Path=templateName}" Stretch="Fill" Width="320" Height="320" x:Name="imagegrid" />
    thanks 

    Tuesday, October 8, 2013 4:28 PM

Answers

  • you need to convert the byte array to image object , use this method

    public BitmapImage base64image(byte[] fileBytes) { using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length)) { ms.Write(fileBytes, 0, fileBytes.Length); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(ms); return bitmapImage; }

    }


    Tuesday, October 8, 2013 7:25 PM
  • Hello Aurelien,

    then try this for get image from byte array:

    using System.Runtime.InteropServices.WindowsRuntime;
    private async Task convertimg(byte[] bytearray)
    {
    BitmapImage img = new BitmapImage();
                var stream1 = new InMemoryRandomAccessStream();
                await stream1.WriteAsync(bytearray1.AsBuffer());
                stream1.Seek(0);
                img.SetSource(stream1);
    
    }

    And another your question of abc is, that abc is variable, that you get your data (byte, id,...) from sqlite. For more information for sqlite for windows phone, show this thread :

    Windows-Phone-7-Silverlight-Programming-Isolated-Storage

    Thursday, October 10, 2013 4:12 AM

All replies

  • you need to convert the byte array to image object , use this method

    public BitmapImage base64image(byte[] fileBytes) { using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length)) { ms.Write(fileBytes, 0, fileBytes.Length); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(ms); return bitmapImage; }

    }


    Tuesday, October 8, 2013 7:25 PM
  • thanks, and how use this method to show in xaml image?

    Wednesday, October 9, 2013 10:08 AM
  • Hello Aurelien,

     public class c
        {
            public BitmapImage img1 { get;set; }
        }

    Try to add one property to your class "BitmapImage".

    ObservableCollection<c> c1 = new ObservableCollection<c>();
    foreach(var a in abc)
    {
    BitmapImage a1 = base64image(a.Imagem);
    c.Add(new c{img1 =a1});
    
    }
    
    public BitmapImage base64image(byte[] fileBytes)
            {
    
                    using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
                    {
                        ms.Write(fileBytes, 0, fileBytes.Length);
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.SetSource(ms);
                        return bitmapImage;
                    }
    }

    Now you got list of image. Bind to Gridview:

    this.Gridview1.DataContext =c1 ;

    Design :

    <GridView x:Name="Gridview1" ItemsSource="{Binding c}">
     <GridView.ItemTemplate>
                            <DataTemplate>
    <Image Source={Binding img1} Width="100" Height="100"/>
     </DataTemplate>
                        </GridView.ItemTemplate>
    
    </GridView>

    Try this.

    • Proposed as answer by Ahmed-Fouad Wednesday, October 9, 2013 9:50 PM
    Wednesday, October 9, 2013 12:52 PM
  • i have some errors, in foreach

    foreach (var a in abc)


    "abc does not exist" and 

    public BitmapImage base64image(byte[] fileBytes)
                    {
    
                        using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
                        {
                            ms.Write(fileBytes, 0, fileBytes.Length);
                            BitmapImage bitmapImage = new BitmapImage();
                            bitmapImage.SetSource(ms);
                            return bitmapImage;
                        }


    bitmapImage.SetSource(ms); "invalid agruments"

    thanks

    Wednesday, October 9, 2013 5:38 PM
  • are you sure that your imgem array bytes represent a valid image ?? can you make sure of that

    • Edited by Ahmed-Fouad Wednesday, October 9, 2013 9:49 PM correction
    Wednesday, October 9, 2013 6:57 PM
  • Hello Aurelien,

    then try this for get image from byte array:

    using System.Runtime.InteropServices.WindowsRuntime;
    private async Task convertimg(byte[] bytearray)
    {
    BitmapImage img = new BitmapImage();
                var stream1 = new InMemoryRandomAccessStream();
                await stream1.WriteAsync(bytearray1.AsBuffer());
                stream1.Seek(0);
                img.SetSource(stream1);
    
    }

    And another your question of abc is, that abc is variable, that you get your data (byte, id,...) from sqlite. For more information for sqlite for windows phone, show this thread :

    Windows-Phone-7-Silverlight-Programming-Isolated-Storage

    Thursday, October 10, 2013 4:12 AM