Binding Image from SQL

2 posts, 1 answers
  1. Albert
    Albert avatar
    13 posts
    Member since:
    Dec 2010

    Posted 10 Jan 2011 Link to this post

    hi telerik Team,

    I've got a hierarchical treeview and would like to display some images on the nodes.
    I've got a imageconverter:




    class ImageConverter : IValueConverter





    public static object ByteToBitmap(byte[] byteImg)





    using (MemoryStream ms = new MemoryStream(byteImg, 0, byteImg.Length))





    //Convert byte[] to image




    Image image = new Image();




    BitmapImage bitmapimage = new BitmapImage();




    MemoryStream stream = new MemoryStream(byteImg);





    return bitmapimage;






    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)





    byte[] byteImg = (byte[])value;




    //Convert byte to image




    return ByteToBitmap(byteImg);



    Part of the xaml file:
    <telerik:HierarchicalDataTemplate x:Key="CustomerTemplate" ItemsSource="{Binding Teams}" ItemTemplate="{StaticResource OrderTemplate}">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="DayID: "/>
    <TextBlock Text="{Binding Date}"/>
    <Image Source="{Binding Image, Converter={StaticResource ImageConverter}}" />





    The images are stored as "Image" in the SQL database.

    I get an object reference on the following line of code:
    using (MemoryStream ms = new MemoryStream(byteImg, 0, byteImg.Length))


    I can't find a solution to retrieve and bind the images that are stored in my database.
    Any help would be appreciated!


  2. Answer
    Petar Mladenov
    Petar Mladenov avatar
    2991 posts

    Posted 13 Jan 2011 Link to this post

    Hello Albert,

    Please find the solution I prepared for you. Basically it uses Ado.Net entity data model and retrieves Employees photo from the MS SQL Express NorthWind database. The solution is based on this article although the project in it uses WCF Ria services.
    Check it out and let us know if it gives you a starting point or if it doesn`t help you, it would be best if you can send us your code so that we could investigate in depth.

    All the best,
    Petar Mladenov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Back to Top