Silverlight 4: How can I convert bmp byte array to

2020-08-03 07:35发布

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条回答
家丑人穷心不美
2楼-- · 2020-08-03 08:33

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

Image Converting

查看更多
登录 后发表回答