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

查看:21
本文介绍了Image to byte[], Convert 和 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);

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

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

字节数组很受应用程序的欢迎,但是当我尝试执行 SetSource 时抛出异常.

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

你能帮我吗?谢谢

推荐答案

此代码有效:

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

谢谢大家:)

这篇关于Image to byte[], Convert 和 ConvertBack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆