在 Windows Phone 7 的 C# 中将 Byte[] 转换为 Image 类型
我在将字节数组转换为图像类型以在 Windows Phone 7 上的应用程序中显示时遇到问题。
数据是从服务器检索的,当我上传和下载数据时,它工作正常,但在转换时我遇到了困难返回为图像格式。
谁能为我解释一下这个问题?
这是我将 Byte 数组转换为 BitmapImage 的方法,
public BitmapImage decodeImage(byte[] array)
{
MemoryStream ms = new MemoryStream(array, 0, array.Length);
// Convert byte[] to Image
ms.Write(array, 0, array.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
然后这是我尝试将返回的 BitmapImage 设置为我在 XAML UI 中使用的图像框的源的代码。
BitmapImage usrIMG = new BitmapImage();
usrIMG = getJson.decodeImage(userProfile.Photos[0].Image);
profileImage.Source = usrIMG;
我知道代码看起来很混乱,而且我正在声明我不需要的东西,我已经摆弄它很多年了,我完全不知所措。
非常感谢
I am having a problem converting a Byte array into an Image type for displaying in an application on Windows Phone 7.
The data is retrieved from a server, and when I upload and download the data it works fine, but I am struggling when converting it back into an Image format.
Can anyone shed some light on this issue for me?
This is my method for turning the Byte array into a BitmapImage,
public BitmapImage decodeImage(byte[] array)
{
MemoryStream ms = new MemoryStream(array, 0, array.Length);
// Convert byte[] to Image
ms.Write(array, 0, array.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
Then this is the code where I try to set the returned BitmapImage to the source for the Image box I am using in the XAML UI.
BitmapImage usrIMG = new BitmapImage();
usrIMG = getJson.decodeImage(userProfile.Photos[0].Image);
profileImage.Source = usrIMG;
I know the code looks mishmashed, and I am declaring things that I dont need to, i have been fiddling with it for ages and I am completely at a loss.
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下代码对我来说可以很好地快速测试您使用 PhotoChooserTask 的场景,并将所选图像存储在字节数组中。您可能还需要检查您存储和检索字节数组的代码,以确保不会丢失任何内容。
the following code works fine for me in a quick test for your scenario of using the PhotoChooserTask, and store the selected image in a byte array. You also might want to review your code where you store and retrieve the byte array on your side, to make sure nothing gets lost there.
您需要一个
WritableBitmap
并了解图像的高度和宽度才能执行此操作。然后你可以做这样的事情:
You'll need a
WritableBitmap
and to know the height and width of the image to be able to do this.Then you can do something like this:
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(..二进制数组数据..));
img1.Source = 位图图像;
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(..Binary array Data..));
img1.Source = bitmapImage;
我之前使用过这段代码,它 100% 成功。
i used this code before and it's work 100% successfuly.