c# - 图像到byte[]、Convert和ConvertBack

标签 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

你能帮帮我吗?谢谢

最佳答案

此代码有效:

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

谢谢大家:)

关于c# - 图像到byte[]、Convert和ConvertBack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9806332/

相关文章:

c# - 如何确定 GIF 中的像素是否透明 (.NET)

linq - 如果我不向数据库插入数据,则在重新启动时获取 SqlCeException

c++ - 缩放位图输出会扭曲图像

image - 我应该在 srcset 属性中包含哪些分辨率?

c# - 在 Windows Phone 7.5 中使用 Silverlight 创建照片库

windows-phone-7 - WP7 GPS启动数据

c# - 无法在我的WPF应用中复制内存

c# - 如何正确序列化自动生成的 WCF 类型

c# - C# 混淆中的变量作用域

图像资源的 Android 标准