0
votes

I have CGBitmap instance (32 bits per pixel) and I would like to get the image data in a byte[].

The byte array should get the values in ARGB format so the first 4 bytes correspond to the first pixel in the image bytes for each of the alpha, red, green and blue values.

1
Check this: stackoverflow.com/questions/448125/… If it's not clear enough let me know I will provide you a sample codeYuri S

1 Answers

2
votes

Here is an idea. image is 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];