Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 256
kite Avatar asked Jul 26 '10 14:07

kite


1 Answers

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;
}
like image 172
Adel Hazzah Avatar answered Nov 08 '22 13:11

Adel Hazzah