При кодировании в UWP Rgba16 выделенного буфера недостаточно
Я кодирую элемент управления Canvas, который содержит Textblocks как дочерний, используя следующий код, но я получаю
The buffer allocated is insufficient
Мой код
using(InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream())
{
var displayInformation = DisplayInformation.GetForCurrentView();
var renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(Textify.CanvasControl);
var width = renderTargetBitmap.PixelWidth;
var height = renderTargetBitmap.PixelHeight;
IBuffer textBuffer = await renderTargetBitmap.GetPixelsAsync();
byte[] pixels = textBuffer.ToArray();
//Encode text to PNG
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);
encoder.SetPixelData(BitmapPixelFormat.Rgba16,
BitmapAlphaMode.Premultiplied,
(uint)width,
(uint)height,
displayInformation.LogicalDpi,
displayInformation.LogicalDpi,
pixels);
await encoder.FlushAsync();
...
Я знаю, что это byte[] pixels
буфер не достаточно большой, так как я хочу закодировать в BitmapPixelFormat.Rgba16
, Я не сталкиваюсь с ошибкой при кодировании в BitmapPixelFormat.Rgba8
или же BitmapPixelFormat.Bgra8
Я попытался вычислить буфер, используя следующее, но он выдает только пустое белое растровое изображение.
byte[] pixels = new byte[16 * width * height];
int index = 0;
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
{
pixels[index++] = 255; // B
pixels[index++] = 255; // G
pixels[index++] = 255; // R
pixels[index++] = 255; // A
}
Я хочу использовать BitmapPixelFormat.Rgba16
потому что я хочу улучшить качество изображения. Как это сделать правильно? Благодарю.