byte[] to BitmapImage in silverlight

kite picture kite · Jul 26, 2010 · Viewed 11.9k times · Source

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

Answer

Adel Hazzah picture Adel Hazzah · Jul 27, 2010

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;
}