我正在使用以下代码对包含 Textblocks 作为子项的 Canvas 控件进行编码,但我得到了
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
,因为我想提高图像质量。如何正确执行此操作?谢谢。