我正在编写一个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
,因为我想提高图像质量。怎么做得好?感谢。