byte[] to BitmapImage in silverlight

牧云@^-^@ 提交于 2019-11-26 17:16:55

问题


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


回答1:


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;
}



回答2:


use this method first use

using System.IO;
using System.Windows.Media.Imaging;

then

 public Image Base64ToImage(byte[] imageBytes)
       {
           Image img = new Image();
           using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
           {
               BitmapImage im = new BitmapImage();
               im.SetSource(ms);
               img.Source = im;
           }
           return img;
       }


来源:https://stackoverflow.com/questions/3335819/byte-to-bitmapimage-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!