none
Convert Binary to Image in C#/WPF

    Question

  • How do you convert a Binary to Image in C#?  I have a DB with Binary column that includes data for an image.  I would like to load and convert to an image to view in WPF app.  Thanks.

    Wednesday, March 04, 2009 7:17 PM

Answers

  • Assuming you have array of bytes for the image that you want to display in WPF image control , here is code.

    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;

     


    Deval Chauhan
    • Marked as answer by shaggygi Thursday, March 05, 2009 12:12 PM
    Thursday, March 05, 2009 4:36 AM

All replies

  • http://www.eggheadcafe.com/community/aspnet/2/10038026/try-this.aspx

    Maybe this helps?
    urban.michal@o2.pl | Feel free to correct my english. I'll be thankful!
    • Proposed as answer by Michał Urban Wednesday, March 04, 2009 7:24 PM
    • Unproposed as answer by shaggygi Wednesday, March 04, 2009 7:31 PM
    Wednesday, March 04, 2009 7:24 PM
  • I came across this site.  Not enough info and does not work with WPF.  Thanks for reply though.
    Wednesday, March 04, 2009 7:32 PM
  • Assuming the binary data has already been decoded, check out BitmapSource.Create()
    Wednesday, March 04, 2009 11:02 PM
  • Assuming you have array of bytes for the image that you want to display in WPF image control , here is code.

    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;

     


    Deval Chauhan
    • Marked as answer by shaggygi Thursday, March 05, 2009 12:12 PM
    Thursday, March 05, 2009 4:36 AM
  • That did the trick.  Thank you very much.
    Thursday, March 05, 2009 12:12 PM
  • You made my day olaaaahhhhhhhhhh!!!!
    Wednesday, March 02, 2011 3:40 PM