C# UWP10 中 WriteableBitmap 中的 Byte[] 不正确 [英] Incorrect Byte[] from WriteableBitmap in C# UWP10

查看:9
本文介绍了C# UWP10 中 WriteableBitmap 中的 Byte[] 不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 WriteableBitmap 中获取 base64 字符串.我认为 byte[] 是不正确的.

I want to get the base64 string from WriteableBitmap. I believe the byte[] to be incorrect.

因为:

用于从 base64 创建图像的代码正在运行.从文件发送 base64 字符串时对此进行了测试.但是,当我将 WriteableBitmap 函数用于 base64 时,我什么也看不到.

The code for creating image from base64 is working. Tested this when sending base64 string from file. However i can't see anything when i'm using my function for WriteableBitmap to 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);
        }

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

Is the byte[] in the wrong format? If so how do i correct it?

我的新尝试

        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.

This attempt doesn't change my byte[] to the correct fromat.

推荐答案

下面的方法创建了一个 BitmapEncoderInMemoryRandomAccessStream 进行操作,添加一个 SoftwareBitmap<从 WriteableBitmap 创建的/code>,将流内容读入字节数组,最后将该字节数组转换为 base64 字符串:

The method below creates a BitmapEncoder that operates on an InMemoryRandomAccessStream, adds a SoftwareBitmap that is created from a WriteableBitmap, reads the stream content into a byte array, and finally converts that byte array into a base64 string:

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);
    }
}

这篇关于C# UWP10 中 WriteableBitmap 中的 Byte[] 不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆