[英]Incorrect Byte[] from WriteableBitmap in C# UWP10
I want to get the base64 string from WriteableBitmap. 我想从WriteableBitmap获取base64字符串。 I believe the byte[] to be incorrect. 我相信byte []是不正确的。
Because: 因为:
The code for creating image from base64 is working. 从base64创建图像的代码正在运行。 Tested this when sending base64 string from file. 从文件发送base64字符串时进行了测试。 However i can't see anything when i'm using my function for WriteableBitmap to base64. 但是,当我将我的函数用于WriteableBitmap到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? byte []的格式是否错误? 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. 这种尝试不会将我的byte []更改为正确的fromat。
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: 下面的方法创建一个在InMemoryRandomAccessStream
上运行的BitmapEncoder
,添加一个从WriteableBitmap
创建的SoftwareBitmap
,将流内容读取到一个字节数组,最后将该字节数组转换为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);
}
}
If you want to convert an image to a base64 stream, the best way would be to encode it first. 如果要将图像转换为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];
}
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.