CGBitmapContext forces me to premultiplied the pixels, i need the raw data as it is in the texture

chrisfs313chrisfs313 PEMember
edited October 2012 in Xamarin.iOS

Hi everyone,

i'm new in the mono touch world =), and i'm very impressed with the work you are doing every day, making this even better, but now i have a question.

Here is my code for loading an image:

string pathName = "some_url";

UIImage imageRaw = UIImage.FromFile (pathName);
CGImage image = imageRaw.CGImage;

rawImageData = new byte[image.Height * (image.Width * 4)];

System.Drawing.RectangleF rectDraw;
rectDraw.X = 0;
rectDraw.Y = 0;
rectDraw.Width = image.Width;
rectDraw.Height = image.Height;

CGBitmapContext context = new CGBitmapContext (rawImageData,
    image.Width, image.Height, image.BitsPerComponent, image.BytesPerRow,
    image.ColorSpace, CGImageAlphaInfo.PremultipliedLast);
context.DrawImage (rectDraw, image);

This code successfully load my image to the memory buffer, and if i draw it on screen, the image is 100% OK. But what if you use the pixels(this has 4 components RGBA) of the image for packing you own information ?? All that data will be corrupted because is premultiplied by the alpha value, so the pixels will have other values, visually will look almost the same but the original information is lost.

Is there a way i can load an image without using the CGImageAlphaInfo.PremultipliedLast and keeping the original pixels data of the image ???

NOTE: To see the pixel data i use an image tool called "Gimp or SeaShore", and i compared the pixels looking at the "rawImageData" variable.

Thanks in advance =).

Cristian Flores Shute.


  • AndrewWayAndrewWay Xamurai USXamarin Team Xamurai
    edited October 2012

    Hey Cristian,
    I couldn't figure out what's going on with CGImage / UIImage. So instead, I found a C# PNG Library, that you can try:

    PngReader pngr = FileHelper.CreatePngReader (pathName);
    var row = pngr.GetRow (0);
    for (int i = 0; i < row.Scanline.Length; i++)
        var R = row.Scanline[i];
        var G = row.Scanline[++i];
        var B = row.Scanline[++i];
        var A = row.Scanline[++i];
        System.Console.WriteLine("RGBA: {0}{1}{2}{3}",R.ToString(), G.ToString(),     B.ToString(), A.ToString());
  • chrisfs313chrisfs313 PEMember

    thanks andrew! this works for me =)

Sign In or Register to comment.