简体   繁体   English

CGBitmapContext构造函数中的参数无效

[英]Invalid parameters in CGBitmapContext constructor

I'm trying to create a blank bitmap in a Xamarin.iOS project using the CGBitmapContext constructor(s) . 我正在尝试使用CGBitmapContext构造函数在Xamarin.iOS项目中创建空白位图。 However, no matter what I try I just get the error "System.Exception: Invalid parameters to context creation" 但是,无论我尝试什么,我都会收到错误"System.Exception: Invalid parameters to context creation"

Example code: 示例代码:

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))
{
    // ...
}

I have tried changing most of the parameters; 我尝试更改大多数参数; I tried fixing the data block and passing it as an IntPtr. 我尝试修复数据块并将其作为IntPtr传递。 I tried using null as the first parameter so that the system would allocate the data. 我尝试使用null作为第一个参数,以便系统分配数据。 And I've tried various flags in the last parameter. 我在最后一个参数中尝试了各种标志。 I always get that same error. 我总是遇到同样的错误。 What parameter is wrong? 哪个参数错误? And what needs to be changed in the code above to make it execute? 为了使它执行,需要在上面的代码中进行哪些更改?

I changed last parameter from CGBitmapFlags.ByteOrderDefault to CGBitmapFlags.PremultipliedLast , and the error disappear. 我将最后一个参数从CGBitmapFlags.ByteOrderDefault更改为CGBitmapFlags.PremultipliedLast ,错误消失了。

refer to CGBitmapFlags Enumeration 参考CGBitmapFlags枚举

As the link said 正如链接所说

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. 此枚举确定数据在内存中的组织,并包括颜色模型,是否存在alpha通道以及组件值是否已预乘。

I think we have to select the corresponding flag to match the data info, especially the color model. 我认为我们必须选择相应的标志来匹配数据信息,尤其是颜色模型。

For the same reason, If you choose CreateDeviceCmyk , CGBitmapFlags.None will be appropriate. 出于相同的原因,如果选择CreateDeviceCmyk ,则CGBitmapFlags.None将不适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM