如何获取CGBitmap的像素数据为Xamarin iOS中的byte []
问题描述:
我有CGBitmap
实例(每像素32位),我想要获取图像数据在byte[]
。如何获取CGBitmap的像素数据为Xamarin iOS中的byte []
的字节数组应该得到的值ARGB格式,以便前4个字节对应于所述第一像素中的每个alpha,红色,绿色和蓝色值的图像字节。
非常感谢!
答
这是一个想法。图像的UIImage
CGImage imageRef = image.CGImage;
int width = (int)image.Size.Width;
int height = (int)image.Size.Height;
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
byte[] rawData = new byte[height * width * 4];
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = 8;
CGContext context = new CGBitmapContext(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big);
context.DrawImage((CGRect)new CGRect(0, 0, width, height), (CGImage)imageRef);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int pixelInfo = (width * y + x) * 4; // The image is png
//red,green, blue,alpha
byte red = rawData[pixelInfo];
byte green = rawData[pixelInfo+1];
byte blue = rawData[pixelInfo+2];
byte alpha = rawData[pixelInfo + 3];
检查:https://*.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core- 1262893#1262893如果不够清楚,让我知道我会为你提供一个示例代码 –