【问题标题】:How to convert an image to byte array in windows phone 8.1 using c#如何使用 c# 在 windows phone 8.1 中将图像转换为字节数组
【发布时间】:2015-01-12 22:59:57
【问题描述】:

我正在使用此代码

MemoryStream ms = new MemoryStream();
WriteableBitmap wb = new WriteableBitmap(myimage);
wb.SaveJpeg(ms, myimage.PixelWidth, myimage.PixelHeight, 0, 100);
byte [] imageBytes = ms.ToArray();

来自Convert image to byte array on Windows Phone 7 No System.Drawing Dll any other way? 但WriteableBitmap 在Windows phone 8.1 环境中不包含SaveJpeg 方法。

【问题讨论】:

  • 一定有什么东西让你觉得它不起作用。
  • 您的目标是 Silverlight 还是运行时 - 取决于此有不同的方法来保存位图。

标签: c# windows-phone-8.1


【解决方案1】:

Windows Phone 8.1 Silverlight 应用程序上,您的代码工作正常,我假设myImageBitmapImage。唯一必须做的就是等待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 Store App,在这种情况下你可以使用以下代码:

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,因为你没有找到the namespace suggested in this answer (which will work for WP8.1 Silverlight)

    在 Windows 运行时应用程序中,您可以使用 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 如果我可以问 - 怎么样?也许你可以给自己添加一个答案?
    • 当然@Romasz。我的完整代码: XAML:
    • BitmapImage bitmapImage = new BitmapImage(new Uri("lh6.googleusercontent.com/…)); RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource); var streamWithContent = await rasr.OpenReadAsync(); byte[] 缓冲区= new byte[streamWithContent.Size]; 等待 streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
    • @Romasz 没有说明如何将图像数据设置为可写位图。您正在创建具有图像宽度和高度的可写位图。 'yourWb.pixelbuffer' 中没有数据。
    【解决方案3】:

    SaveJpeg 是一种扩展方法 (http://msdn.microsoft.com/en-US/library/windowsphone/develop/system.windows.media.imaging.extensions.savejpeg(v=vs.105).aspx)。您是否在项目引用中引用了Microsoft.Phone 并将using System.Windows.Media.Imaging; 添加到您的.cs 文件中?

    【讨论】:

    • 感谢@tster 的快速回复。我没有找到你提到的 Microsoft.Phone 参考。你建议我如何以另一种方式实现这一目标。
    • @ababil,尝试在 Visual Studio 中创建一个“Windows Phone”项目。这将附带 Microsoft.Phone 引用。
    • Microsoft.Phone 命名空间不再存在于 Windows Phone 8.1 平台中。
    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多