views:

256

answers:

2

Hi,

I'm using Marshal.Copy() to copy the pixel information from a Bitmap to an int[] array, the problem resides in the fact that the information that is coming to this array is coming all wrong, like:

             [0] = -8682109; 
             [1] = -8682109; 
             [2] = -8616573; 
             [3] = -8616573; 
             [4] = -8550527; 
             and so on...

the code for the method is:

    private unsafe int[] BmpToBytes_Unsafe(Bitmap bmp)
    {
        BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
            ImageLockMode.ReadOnly,
            PixelFormat.Format32bppRgb);

        // number of bytes in the bitmap
        byteCount = bData.Stride * (bmp.Height);


        int[] bytes = new int[byteCount / 4];


        Marshal.Copy(bData.Scan0, bytes, 0, byteCount/4);


        // don't forget to unlock the bitmap!!
        bmp.UnlockBits(bData);

        return bytes;

When I was using a byte[] array, the information that was stored was correct, so I don't know what is happening here.

Anyone can help me?

A: 

IMHO, your bytecount is a number of byte's and now you're dividing bytes by 4 and expecting a set of integers, that are really bytes. The cast is not performed as you want it.

To convert a byte array to int array use:

System.BitConverter.ToInt32(mybyteArray,4);

Tony
+1  A: 

Those values are perfectly normal. They are 32bpp pixels with the alpha channel at 0xff, the usual value. In other words: -8682109 == 0xff7b8583. Any alpha value >= 128 will make the value negative because it sets the sign bit.

Using uint[] instead of int[] can make that easier to see.

Hans Passant
Btw, you forgot to take care of your previous question.
Hans Passant
The problem is that the method Marshal.Copy doesn't take uint[], do you know how could I manipulate that value, separating the RGB components?
Staticsoul
Use Color.FromArgb(), it takes an int.
Hans Passant