在 Windows Phone 7 的 C# 中将 Byte[] 转换为 Image 类型

发布于 2024-10-30 17:33:36 字数 776 浏览 1 评论 0原文

我在将字节数组转换为图像类型以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-11-06 17:33:36

以下代码对我来说可以很好地快速测试您使用 PhotoChooserTask 的场景,并将所选图像存储在字节数组中。您可能还需要检查您存储和检索字节数组的代码,以确保不会丢失任何内容。

private byte[] imageBytes;
private void GetPhoto_Click(object sender, RoutedEventArgs e)
{
    PhotoChooserTask photoTask = new PhotoChooserTask();
    photoTask.Completed += new EventHandler<PhotoResult>(photoTask_Completed);
    photoTask.Show();
}

void photoTask_Completed(object sender, PhotoResult e)
{
    imageBytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

    // save 'imageBytes' byte array to data base ...
}

private void ShowPhoto_Click(object sender, RoutedEventArgs e)
{
    // load 'imageBytes' byte array to data base ...
    BitmapImage bitmapImage = new BitmapImage();
    MemoryStream ms = new MemoryStream(imageBytes);
    bitmapImage.SetSource(ms);
    myImageElement.Source = bitmapImage;
}

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.

private byte[] imageBytes;
private void GetPhoto_Click(object sender, RoutedEventArgs e)
{
    PhotoChooserTask photoTask = new PhotoChooserTask();
    photoTask.Completed += new EventHandler<PhotoResult>(photoTask_Completed);
    photoTask.Show();
}

void photoTask_Completed(object sender, PhotoResult e)
{
    imageBytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

    // save 'imageBytes' byte array to data base ...
}

private void ShowPhoto_Click(object sender, RoutedEventArgs e)
{
    // load 'imageBytes' byte array to data base ...
    BitmapImage bitmapImage = new BitmapImage();
    MemoryStream ms = new MemoryStream(imageBytes);
    bitmapImage.SetSource(ms);
    myImageElement.Source = bitmapImage;
}
桃酥萝莉 2024-11-06 17:33:36

您需要一个 WritableBitmap 并了解图像的高度和宽度才能执行此操作。
然后你可以做这样的事情:

var result = new WriteableBitmap(width, height);
var ms = new MemoryStream();
ms.Write(myByteArray, myByteArray, myByteArray.Length);
result.SetSource(ms);

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 result = new WriteableBitmap(width, height);
var ms = new MemoryStream();
ms.Write(myByteArray, myByteArray, myByteArray.Length);
result.SetSource(ms);
不疑不惑不回忆 2024-11-06 17:33:36

var bitmapImage = new BitmapImage();

bitmapImage.SetSource(new MemoryStream(..二进制数组数据..));

img1.Source = 位图图像;

var bitmapImage = new BitmapImage();

bitmapImage.SetSource(new MemoryStream(..Binary array Data..));

img1.Source = bitmapImage;

罪#恶を代价 2024-11-06 17:33:36
public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
        {
            MemoryStream stream = new MemoryStream(byteArray);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(stream);
            return bitmapImage;
        }

我之前使用过这段代码,它 100% 成功。

public BitmapImage ByteArraytoBitmap(Byte[] byteArray)
        {
            MemoryStream stream = new MemoryStream(byteArray);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(stream);
            return bitmapImage;
        }

i used this code before and it's work 100% successfuly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文