“无效的句柄"创建CGBitmapContext [英] "Invalid Handle" Create CGBitmapContext
问题描述
我在CGBitmapcontext上遇到了问题. 使用消息无效的句柄"创建CGBitmapContext时出现错误.
I've got a problem with the CGBitmapcontext. I get en error while creating the CGBitmapContext with the message "invalid Handle".
这是我的代码:
var previewContext = new CGBitmapContext(null, (int)ExportedImage.Size.Width, (int)ExportedImage.Size.Height, 8, (int)ExportedImage.Size.Height * 4, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst);
谢谢;
推荐答案
这是因为您要将null传递给第一个参数. CGBitmapContext用于直接绘制到内存缓冲区中.构造函数所有重载中的第一个参数是(Apple docs):
That is because you are passing null to the first parameter. The CGBitmapContext is for drawing directly into a memory buffer. The first parameter in all the overloads of the constructor is (Apple docs):
数据 指向内存中要绘制图形的目标的指针.该内存块的大小至少应为 (bytesPerRow * height)个字节.
data A pointer to the destination in memory where the drawing is to be rendered. The size of this memory block should be at least (bytesPerRow*height) bytes.
在MonoTouch中,为方便起见,我们得到两个接受byte []的重载.因此,您应该像这样使用它:
In MonoTouch, we get two overloads that accept a byte[] for convenience. So you should use it like this:
int bytesPerRow = (int)ExportedImage.Size.Width * 4; // note that bytes per row should
//be based on width, not height.
byte[] ctxBuffer = new byte[bytesPerRow * (int)ExportedImage.Size.Height];
var previewContext =
new CGBitmapContext(ctxBuffer, (int)ExportedImage.Size.Width,
(int)ExportedImage.Size.Height, 8, bytesPerRow, colorSpace, bitmapFlags);
这篇关于“无效的句柄"创建CGBitmapContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!