如何在Windows Phone 8.1中使用C#将图像转换为字节数组

4

一定有什么原因让你认为它不起作用。 - mbm
你是针对Silverlight还是Runtime进行开发 - 根据不同的目标,有不同的方法来保存位图。 - Romasz
3个回答

3
在Windows Phone 8.1银光应用中,您的代码可以正常工作。我假设myImage是一个BitmapImage。您唯一需要做的就是等待BitmapImage完全加载:
using System.IO;
using System.Windows.Media.Imaging;
...
myImage = new BitmapImage(new Uri("http://i.stack.imgur.com/830Ke.jpg?s=128&g=1", UriKind.Absolute));
myImage.CreateOptions = BitmapCreateOptions.None;
myImage.ImageOpened += myImage_ImageOpened;
...
void myImage_ImageOpened(object sender, RoutedEventArgs e)
{
    MemoryStream ms = new MemoryStream();
    WriteableBitmap wb = new WriteableBitmap(myImage);
    wb.SaveJpeg(ms, myImage.PixelWidth, myImage.PixelHeight, 0, 100);
    byte[] imageBytes = ms.ToArray();
}

我刚刚测试了这段代码,它在WP8.1上完美运行。

但是正如你在另一篇帖子中所评论的,你无法引用Microsoft.Phone,你可能正在做一个Windows Phone 8.1商店应用程序,在这种情况下,你可以使用以下代码:

using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage.Streams;
using System.Runtime.InteropServices.WindowsRuntime;
...
BitmapImage bitmapImage = new BitmapImage(new Uri("http://i.stack.imgur.com/830Ke.jpg?s=128&g=1"));
RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
var streamWithContent = await rasr.OpenReadAsync();
byte[] buffer = new byte[streamWithContent.Size];
await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);

2
我猜测你的目标是Windows Runtime Apps,因为你没有找到这个答案建议使用的名称空间(它适用于WP8.1 Silverlight)

在Windows Runtime应用中,你可以使用Encoder,代码示例可以如下所示:

// lets assume that you have your image in WriteableBitmap yourWb
using (MemoryStream ms = new MemoryStream())
{
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms.AsRandomAccessStream());
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Ignore,
        imageWidth,
        imageHeight,
        horizontalDPI,
        verticalDPI,
        yourWb.PixelBuffer.ToArray());

    await encoder.FlushAsync();
}

感谢@Romasz的回复。我另外找到了解决问题的方法。 - ababil
@ababil 如果我可以问 - 怎么做?也许你可以添加一个答案? - Romasz
当然,@Romasz。我的完整代码: XAML: <Button Grid.Row="1" x:Name="btnSubmit" Content="提交" Click="btnSubmit_Click"/> <Image Grid.Row="2" x:Name="imagePreivew" /> - ababil
BitmapImage bitmapImage = new BitmapImage(new Uri("https://lh6.googleusercontent.com/ImYzXmXzbrWHt5wsrRkN_53OWH9nP6y_jqvH3nbXIQ=s207-p-no?s=128&g=1")); RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource); var streamWithContent = await rasr.OpenReadAsync(); byte[] buffer = new byte[streamWithContent.Size]; await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None); - ababil
@Romasz 没有关于如何将图像数据设置为可写位图的说明。您正在使用图像的宽度和高度创建可写位图。而且 'yourWb.pixelbuffer' 中没有任何数据。 - MohanRajNK
@MohanRajNK 是的,你说得对,但这只是一个示例,以展示正确的方向。如果你正在寻找如何获取像素缓冲区,可以查看这个答案 - Romasz

0

感谢@tster的快速回复。我没有找到你提到的Microsoft.Phone参考。你能建议我另外的方法吗? - ababil
@ababil,尝试在Visual Studio中创建一个“Windows Phone”项目。这将包含Microsoft.Phone的引用。 - tster
在Windows Phone 8.1平台中,Microsoft.Phone命名空间不再存在。 - NadaNK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接