Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
20
byteArray[] as image column
posted

What would be the best way to format a column in the xamDataGrid to display images which are stored as byteArray[] in a database.

 

Thanks,

Nate

  • 2677
    posted

    Hello npoling,

    If they are stored in the db as binary, you will have to convert it over to an image.  So, depending on how you bring in your data, whether it be via service or any other means, you will have to take the binary data and convert it.  So, if you are using the MVVM approach, when you hydrate your model classes, you can have a mapper that will map the DTO class to the model class.  In there, you can translate the binary to an image similar to the code below. 

    byte[] imageArr ;

    //set your imageArr here---

    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.CreateOptions = BitmapCreateOptions.None;
    bi.CacheOption = BitmapCacheOption.Default;
    bi.StreamSource = new MemoryStream
    (imageArr);
    bi.EndInit();

    Image img = new Image();
      //Image control of wpf

    img.Source = bi;

    Now, you model class should have an image property called Photo(can be anything) that exposes img.  Then just bind the field in the grid to that property.

     If you are reading straight from the db, you may have to create an unbound column in the datatable and set the values to the converted bytearray to image with similar code below.  Then just point a field in the grid  to the new unbound column key that we added similar to the code above. 

    That should be all you have to do.  If you have any questions, please let me know.  If you have a specific way you want to bring the data back, let me know and I will give you the best approach for it.