Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can we convert silverlight bitmapimage to byte array and byte array to bitmapimage
Posted

1 solution

Hi there,
Convert BitmapImage to byte array:
C#
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder(); //Replace JPeg for your BitmapEncoder (Pnh, Gif, etc.)
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    data = ms.ToArray();
}

or
C#
public byte[] imageToByte(System.Drawing.Image img)
{
MemoryStream objMS = new MemoryStream();
img.Save(objMS,System.Drawing.Imaging.ImageFormat.Gif);
return  objMS.ToArray();
}

Convert byte array to BitmapImage:
C#
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;
}


Hope it helps.
 
Share this answer
 
v2
Comments
sawshalin 23-Jul-14 3:29am    
He's asking about silverlight !!!! there is no System.Drawing or save method for BitMapImage
dileep Perumbavoor 23-Sep-15 4:25am    
For Convert byte array to BitmapImage:I am getting catastrophic failure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900