Silverlight 4: How can I convert bmp byte array to

2020-08-03 07:56发布

问题:

I have a wcf service which returns a bmp in byte[]. However Silverlight's Image control doesnt support displaying bmp's so i need to convert the bmp byte[] to png or jpg byte[]. Is there a library out there which does this conversion? Or any other way of displaying the bmp byte[] on the silverlight client?

Thanks!

Update1

In order to achieve the conversion I would have done something like this in .NET

private byte[]  ConvertBmpToJpeg(byte[] bmp)
{
    using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bmp)))
    {
        MemoryStream ms = new MemoryStream();                
        image.Save(ms, ImageFormat.Jpeg);
        return ms.ToArray();
    }
 }

Since System.Drawing is not available in Silverlight, how do I achieve what the code does above in Silverlight?

Answer using the library mentioned by dj kraze below-

        ExtendedImage img = new ExtendedImage();
        var bd = new BmpDecoder();
        var je = new JpegEncoder();
        bd.Decode(img, new MemoryStream(bitmapBytes));
        MemoryStream ms = new MemoryStream();
        je.Encode(img, ms);

        BitmapImage bi = new BitmapImage();
        bi.SetSource(new MemoryStream(ms.ToArray()));
        display_ScreenShot.Source = bi;

回答1:

Here is an even easier way of doing it.. This site may help out a lot

Image Converting