Explain this C# code: byte* p = (byte*) (void*) Scan0;

Posted by qulzam on Stack Overflow See other posts from Stack Overflow or by qulzam
Published on 2009-10-30T02:42:18Z Indexed on 2010/04/03 19:43 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

I found the code from the net in which i cant understand this line:-

byte* p = (byte*)(void*)Scan0;

There Scan0 is System.IntPtr. It is code of C#.Net. Plz Explain the above line.

The complete code is given below. this is code to convert a image in grayscale.

public static Image GrayScale(Bitmap b)
    {

        BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

        int stride = bmData.Stride;
        System.IntPtr Scan0 = bmData.Scan0;

        unsafe
        {
            byte* p = (byte*)(void*)Scan0;

            int nOffset = stride - b.Width * 3;

            byte red, green, blue;

            for (int y = 0; y < b.Height; ++y)
            {
                for (int x = 0; x < b.Width; ++x)
                {
                    blue = p[0];
                    green = p[1];
                    red = p[2];

                    p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);

                    p += 3;
                }
                p += nOffset;
            }
        }

        b.UnlockBits(bmData);

        return (Image)b;
    }

I understand all the code but only have the problem on this line.

byte* p = (byte*)(void*)Scan0;

© Stack Overflow or respective owner

Related posts about c#

Related posts about pointers