byte[] to BitmapImage in silverlight

前端 未结 2 1095
终归单人心
终归单人心 2020-12-03 23:21

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 a

相关标签:
2条回答
  • 2020-12-04 00:01

    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;
    }
    
    0 讨论(0)
  • 2020-12-04 00:01

    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;
           }
    
    0 讨论(0)
提交回复
热议问题