CGBitmapContext 构造函数中的参数无效

Invalid parameters in CGBitmapContext constructor

我正在尝试使用 CGBitmapContext constructor(s) 在 Xamarin.iOS 项目中创建空白位图。但是,无论我尝试什么,我都会得到错误 "System.Exception: Invalid parameters to context creation"

示例代码:

const int width = 100;
const int height = 100;
const int bytesPerPixel = 4;
const int bitsPerComponent = 8;
var intBytes = width * height * bytesPerPixel;
byte[] pixelData = new byte[intBytes];
var colourSpace = CGColorSpace.CreateDeviceRGB();
using (var objContext = new CGBitmapContext(pixelData, width, height, bitsPerComponent, width * bytesPerPixel, colourSpace, CGBitmapFlags.ByteOrderDefault))
{
    // ...
}

我试过更改大部分参数;我尝试修复数据块并将其作为 IntPtr 传递。我尝试使用 null 作为第一个参数,以便系统分配数据。我在最后一个参数中尝试了各种标志。我总是得到同样的错误。什么参数错了?上面的代码需要修改什么才能执行?

我将最后一个参数从 CGBitmapFlags.ByteOrderDefault 更改为 CGBitmapFlags.PremultipliedLast ,错误消失了。

参考CGBitmapFlags Enumeration

正如link所说

This enumeration specifies the layout information for the component data in a bitmap.

This enumeration determines the in-memory organization of the data and includes the color model, whether there is an alpha channel present and whether the component values have been premultiplied.

我想我们必须select相应的标志来匹配数据信息,尤其是颜色模型。

同理,如果选择CreateDeviceCmyk,则CGBitmapFlags.None为宜。