简体   繁体   English

图像到字节[],Convert和ConvertBack

[英]Image to byte[], Convert and ConvertBack

I have a service that converts images stored on a website to byte array 我有一项将网站上存储的图像转换为字节数组的服务

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

This code returns a byte array that I store in a database (SQL Azure). 此代码返回一个字节数组,该字节数组存储在数据库(SQL Azure)中。 In my Windows Phone application, I try to convert this byte array to display it on my page. 在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();
    }
}

The byte array is well received by the application, but an exception is thrown when I try to do a SetSource. 字节数组被应用程序很好地接收,但是当我尝试执行SetSource时会引发异常。

empImage.SetSource(new MemoryStream((Byte[])value));
=> "Exception was unhandled", The request is not supported

Can you help me? 你能帮助我吗? Thx 谢谢

This code works : 此代码有效:

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

Thank you everyone :) 谢谢大家 :)

these questions have already been answered on stackoverflow 这些问题已经在stackoverflow上得到了解答
for the image to a byte[] try this 为图像到一个字节[]试试这个
and for the byte[] to image try this 并为字节[]图像试试这个

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

For my UWP app i use the following IValueConverter to convert a byte array to a bindable object for an <Image Source={Binding} /> 对于我的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;
}

Excuse me for the WriteAsync in a synchronous function. 对不起,我使用同步功能中的WriteAsync。 For the purpose of this post I don't have the time to solve this one, but it works this way :) 为了这篇文章的目的,我没有时间解决这个问题,但是它是这样工作的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM