none
WindowsPhone将接收到的byte数组转换成图片 RRS feed

  • 问题

  • Encoding.UTF8.GetString(receiveArgs.Buffer, receiveArgs.Offset, receiveArgs.BytesTransferred);

    我从C#服务器接收到receiveArgs.Buffer字节数组,想把它转换成图片直接显示在下面的控件中

    <Image Name="Imagine1" HorizontalAlignment="Left" Height="131" VerticalAlignment="Top" Width="213"/>

    以下是我显示图片的代码:

    byte[] id = receiveArgs.Buffer;
                   MemoryStream ms = new MemoryStream(id);
                                      Image bmp = new Image();
                                      bmp.SetSource(ms);
    
                                    
                                     Imagine1.Source = bmp;

    但是显示的是一些混乱的颜色,求问是怎么回事?该怎么写?

    小弟初来乍到,求各位大神多多指点
    2014年3月15日 15:24

答案

  • 你好,

    >>但是显示的是一些混乱的颜色

    代码看上去是没有问题的。例子:

      private void ArrayToImage()
            {
                using (MemoryStream ms = new MemoryStream(bytearray))
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(ms);
                    img01.Source = bitmapImage;
                }
            }

    尝试用Reverse函数将你得到的bytes反转一下再进行一系列操作。

    Array.Reverse(bytes, 0, bytes.Length);


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年3月17日 8:18
    版主