我想在我的 Windows 8 应用程序中创建一个白色图像运行时。我想我会创建一个字节数组,然后写入文件。但我得到了例外The buffer allocated is insufficient. (Exception from HRESULT: 0x88982F8C)
。我的代码有什么问题?
double dpi = 96;
int width = 128;
int height = 128;
byte[] pixelData = new byte[width * height];
for (int y = 0; y < height; ++y)
{
int yIndex = y * width;
for (int x = 0; x < width; ++x)
{
pixelData[x + yIndex] = (byte)(255);
}
}
var newImageFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("white.png", CreationCollisionOption.GenerateUniqueName);
using (IRandomAccessStream newImgFileStream = await newImageFile.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder bmpEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, newImgFileStream);
bmpEncoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)width,
(uint)height,
dpi,
dpi,
pixelData);
await bmpEncoder.FlushAsync();
}