gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:14:56 24 4
gpt4 key购买 nike

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

                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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com