[英]Incorrect Byte[] from WriteableBitmap in C# UWP10
我想從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);
}
byte []的格式是否錯誤? 如果是這樣,我該如何糾正?
我的新嘗試
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。
下面的方法創建一個在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);
}
}
如果要將圖像轉換為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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.