Image to byte[], Convert and ConvertBack

徘徊边缘 提交于 2019-12-30 11:06:28

问题


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). In my Windows Phone application, I try to convert this byte array to display it on my page.

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.

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

Can you help me? Thx


回答1:


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




回答2:


these questions have already been answered on stackoverflow
for the image to a byte[] try this
and for the byte[] to image try this




回答3:


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



回答4:


For my UWP app i use the following IValueConverter to convert a byte array to a bindable object for an <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. For the purpose of this post I don't have the time to solve this one, but it works this way :)



来源:https://stackoverflow.com/questions/9806332/image-to-byte-convert-and-convertback

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