kite
kite

Reputation: 679

byte[] to BitmapImage in silverlight

For the purpose of a game, I need to serialize some pictures in a binary file through a WPF application, using bitmapEncoder and its child classes.

But these class are not available in silverlight, so I can't load them into the browser from the same binary file.

Does someone know how to convert a byte[] to BitmapImage in silverlight?

Thanks,

KiTe

Upvotes: 5

Views: 12091

Answers (2)

Amgad Mohamed
Amgad Mohamed

Reputation: 173

use this method first use

using System.IO;
using System.Windows.Media.Imaging;

then

 public Image Base64ToImage(byte[] imageBytes)
       {
           Image img = new Image();
           using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
           {
               BitmapImage im = new BitmapImage();
               im.SetSource(ms);
               img.Source = im;
           }
           return img;
       }

Upvotes: 0

Adel Hazzah
Adel Hazzah

Reputation: 8947

Try something like this:

BitmapImage GetImage( byte[] rawImageBytes )
{
    BitmapImage imageSource = null;

    try
    {
        using ( MemoryStream stream = new MemoryStream( rawImageBytes  ) )
        {
            stream.Seek( 0, SeekOrigin.Begin );
            BitmapImage b = new BitmapImage();
            b.SetSource( stream );
            imageSource = b;
        }
    }
    catch ( System.Exception ex )
    {
    }

    return imageSource;
}

Upvotes: 8

Related Questions