Incorrect Byte[] from WriteableBitmap in C# UWP10

别说谁变了你拦得住时间么 提交于 2019-12-11 13:08:45

问题


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

Because:

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.

My attempts so far.

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

Test example:

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

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

My new attempt

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

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


回答1:


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



回答2:


If you want to convert an image to a base64 stream, the best way would be to encode it first.

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

Test case to show it works:

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

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

image1.Source = decoded;


来源:https://stackoverflow.com/questions/37024751/incorrect-byte-from-writeablebitmap-in-c-sharp-uwp10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!