来自C#UWP10中WriteableBitmap的字节[]不正确

时间:2016-05-04 10:14:23

标签: c# win-universal-app windows-10-universal writablebitmap

我想从WriteableBitmap获取base64字符串。 我认为byte []不正确。

由于:

从base64创建图像的代码正在运行。从文件发送base64字符串时测试过。但是当我使用我的函数WriteableBitmap到base64时,我什么都看不到。

到目前为止我的尝试。

   public static string GetByteArrayFromImage(WriteableBitmap writeableBitmap)
        {
             Stream stream = writeableBitmap.PixelBuffer.AsStream();
             MemoryStream memoryStream = new MemoryStream();
             stream.CopyTo(memoryStream);
             Byte[] bytes = memoryStream.ToArray();
             return Convert.ToBase64String(bytes);
        }

   public static string GetByteArrayFromImage(WriteableBitmap writeableBitmap)
        {
             Byte[] bytes = writeableBitmap.PixelBuffer.ToArray();
             return Convert.ToBase64String(bytes);
        }

测试示例:

   public static async Task<string> GetBase64StringFromFileAsync(StorageFile storageFile)
        {
            Stream ms = await storageFile.OpenStreamForReadAsync();
            byte[] bytes = new byte[(int)ms.Length];
            ms.Read(bytes, 0, (int)ms.Length);
            return Convert.ToBase64String(bytes);
        }

字节[]的格式是否错误?如果是这样,我该如何纠正呢?

我的新尝试

        Stream stream = writeableBitmap.PixelBuffer.AsStream();
        byte[] pixels = new byte[(uint)stream.Length];
        await stream.ReadAsync(pixels, 0, pixels.Length);

        using (var writeStream = new InMemoryRandomAccessStream())
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, writeStream);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96, 96, pixels);
            await encoder.FlushAsync();
        }
        return Convert.ToBase64String(pixels);

此尝试不会将我的byte []更改为正确的fromat。

2 个答案:

答案 0 :(得分:2)

以下方法创建BitmapEncoder,对InMemoryRandomAccessStream进行操作,添加从SoftwareBitmap创建的WriteableBitmap,将流内容读入字节数组,最后将该字节数组转换为base64字符串:

public async Task<string> ToBase64String(WriteableBitmap writableBitmap)
{
    using (var stream = new InMemoryRandomAccessStream())
    {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

        encoder.SetSoftwareBitmap(SoftwareBitmap.CreateCopyFromBuffer(
            writableBitmap.PixelBuffer,
            BitmapPixelFormat.Bgra8,
            writableBitmap.PixelWidth,
            writableBitmap.PixelHeight));

        await encoder.FlushAsync();

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

        return Convert.ToBase64String(bytes);
    }
}

答案 1 :(得分:0)

如果要将图像转换为base64流,最好的方法是首先对其进行编码。

public static string GetByteArrayFromImage(BitmapSource writeableBitmap)
{
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(writeableBitmap));
    MemoryStream stream = new MemoryStream();
    encoder.Save(stream);
    Byte[] bytes = stream.ToArray();
    return Convert.ToBase64String(bytes);
}

public static BitmapSource GetImageFromByteArray(string base64String)
{
    byte[] bytes = Convert.FromBase64String(base64String);
    MemoryStream stream = new MemoryStream(bytes);
    JpegBitmapDecoder decoder = new JpegBitmapDecoder(stream, 
        BitmapCreateOptions.None, BitmapCacheOption.Default);
    return decoder.Frames[0];
}

测试用例表明它有效:

BitmapImage originalImage = new BitmapImage(new Uri(@"Path to any picture", UriKind.Absolute));

string encoded = GetByteArrayFromImage(originalImage);
BitmapSource decoded = GetImageFromByteArray(encoded);

image1.Source = decoded;