简体   繁体   中英

Silverlight 4: How can I convert bmp byte array to png byte array?

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;

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

Image Converting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM