在UWP中将BitmapImage转换为字节数组

时间:2016-03-19 22:36:22

标签: c# winrt-xaml win-universal-app bitmapimage

如何在UWP中将BitmapImage对象转换为字节数组?在.Net中很容易实现,即使在以前的WinRT版本中,在互联网上看起来却没有成功,其中一个解决方案是使用answer中提到的WriteableBitmap,但在当前版本的UWP,从BitmapImage构造WriteableBitmap是不可能的,任何解决方法?

2 个答案:

答案 0 :(得分:10)

因为你是从图片网址开始的,所以我能想出的唯一方法是获取图片流。为此,RandomAccessStreamReference.CreateFromUri()方法非常有用。

Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri("http://...")).OpenReadAsync();

然后我们必须对流进行解码,以便能够读取所有像素供以后使用。

Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();

最后,您可以通过这种方式访问​​像素缓冲区。

byte[] bytes = pixelData.DetachPixelData();

答案 1 :(得分:4)

是的,对于一个字节[]并不太复杂,我想你可以得到它。我希望它更简单,但事实并非如此。

  

http://codepaste.net/ijx28i

这段代码实际上更进一步,将byte []转换为Base64字符串(用于序列化),但是你可以忽略那个额外的步骤,对吗?

// using System.Runtime.InteropServices.WindowsRuntime;

private async Task<string> ToBase64(Image control)
{
    var bitmap = new RenderTargetBitmap();
    await bitmap.RenderAsync(control);
    return await ToBase64(bitmap);
}

private async Task<string> ToBase64(WriteableBitmap bitmap)
{
    var bytes = bitmap.PixelBuffer.ToArray();
    return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
}

private async Task<string> ToBase64(StorageFile bitmap)
{
    var stream = await bitmap.OpenAsync(Windows.Storage.FileAccessMode.Read);
    var decoder = await BitmapDecoder.CreateAsync(stream);
    var pixels = await decoder.GetPixelDataAsync();
    var bytes = pixels.DetachPixelData();
    return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
}

private async Task<string> ToBase64(RenderTargetBitmap bitmap)
{
    var bytes = (await bitmap.GetPixelsAsync()).ToArray();
    return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
}

private async Task<string> ToBase64(byte[] image, uint height, uint width, double dpiX = 96, double dpiY = 96)
{
    // encode image
    var encoded = new InMemoryRandomAccessStream();
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, encoded);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, height, width, dpiX, dpiY, image);
    await encoder.FlushAsync();
    encoded.Seek(0);

    // read bytes
    var bytes = new byte[encoded.Size];
    await encoded.AsStream().ReadAsync(bytes, 0, bytes.Length);

    // create base64
    return Convert.ToBase64String(bytes);
}

private async Task<ImageSource> FromBase64(string base64)
{
    // read stream
    var bytes = Convert.FromBase64String(base64);
    var image = bytes.AsBuffer().AsStream().AsRandomAccessStream();

    // decode image
    var decoder = await BitmapDecoder.CreateAsync(image);
    image.Seek(0);

    // create bitmap
    var output = new WriteableBitmap((int)decoder.PixelHeight, (int)decoder.PixelWidth);
    await output.SetSourceAsync(image);
    return output;
}

这是重要的部分:

var bytes = (await bitmap.GetPixelsAsync()).ToArray();

以为你可能会喜欢看它的背景。

祝你好运。