图像到byte [],Convert和ConvertBack

时间:2012-03-21 14:11:57

标签: c# image windows-phone-7

我有一项服务可以将网站上存储的图像转换为字节数组

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("URLTOIMAGE");
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                Bitmap bmp = new Bitmap(myResponse.GetResponseStream());
                myResponse.Close();
                ms = new MemoryStream();
                bmp.Save(ms, ImageFormat.Bmp);

此代码返回我存储在数据库(SQL Azure)中的字节数组。在我的Windows Phone应用程序中,我尝试转换此字节数组以在我的页面上显示它。

public class BytesToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        BitmapImage empImage = new BitmapImage();
        empImage.SetSource(new MemoryStream((Byte[])value));
        return empImage;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                                System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

应用程序很好地接收了字节数组,但是当我尝试执行SetSource时会抛出异常。

empImage.SetSource(new MemoryStream((Byte[])value));
=> "Exception was unhandled", The request is not supported
你能帮帮我吗? THX

4 个答案:

答案 0 :(得分:3)

此代码有效:

public class BytesToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        MemoryStream stream = new MemoryStream((Byte[])value);
        WriteableBitmap bmp = new WriteableBitmap(173, 173);
        bmp.LoadJpeg(stream);
        return bmp;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                                System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

谢谢大家:)

答案 1 :(得分:2)

这些问题已在stackoverflow上得到解答 对于图像到字节[]尝试this
并且对于byte []来映像,请尝试this

答案 2 :(得分:1)

private ImageSource GetPhoto(byte[] bytearr)
{
   if (bytearr != null)
   {
      BitmapImage image = new BitmapImage();

      InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
      ms.AsStreamForWrite().Write(bytearr, 0, bytearr.Length);
      ms.Seek(0);

      image.SetSource(ms);
      ImageSource src = image;

      return src;
   }
   else
      return null;
}

答案 3 :(得分:0)

对于我的UWP应用程序,我使用以下IValueConverter将字节数组转换为<Image Source={Binding} />的可绑定对象

internal class ByteImageSourceConverter : IValueConverter
{
    object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return null;
        return ByteToImage((byte[])value);
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }

    static ImageSource ByteToImage(byte[] imageBytes)
    {
        BitmapImage image = new BitmapImage();
        image.SetSource(imageBytes.ConvertToInMemoryRandomAcessStream());
        ImageSource src = image;
        return src;
    }
}

internal static InMemoryRandomAccessStream ConvertToInMemoryRandomAcessStream(this byte[] arr)
{
    var randomAccessStream = new InMemoryRandomAccessStream();
    randomAccessStream.WriteAsync(arr.AsBuffer());
    randomAccessStream.Seek(0);
    return randomAccessStream;
}

对于同步函数中的WriteAsync,请原谅。为了这篇文章的目的,我没有时间来解决这个问题,但它的工作方式是这样的:)