0

I've got a delphi dll which has a deal with camera and stores video frames to 3d byte-array inside this dll. 3d dimention is needed to implement rgb format, and it is a convenient for dll background (as developer said). So, I have to access that array from c# code, build a Bitmap and display its content. But i dont understand how to access the array elements properly. Here is my code:

    private unsafe void ByteArray2Bitmap(IntPrt data, int width, int height, int depth, out Bitmap bmp)
    {            
        // create a bitmap and manipulate it
        bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        BitmapData bits = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bmp.PixelFormat);

        fixed(byte*** data = (byte***)(m_data.ToPointer()))
        {
            for (int i = 0; i < height; i++)
            {
                int* row = (int*)((byte*)bits.Scan0 + (i * bits.Stride));
                for (int j = 0; j < width; j++)
                {
                    int pixel = BitConverter.ToInt32(&data[i][j][0], 0);
                    row[j] = pixel;
                }
            }
        }
        bmp.UnlockBits(bits);
    }

That line of code got error: The right hand side of a fixed statement assignment may not be a cast expression

fixed(byte*** data = (byte***)(m_data.ToPointer()))

Is the any way to access multi dimentional unmanaged arrays without copying them with Marshal Copy?

ilja
  • 9
  • 1

1 Answers1

0

Your code is somewhat confusing. You have a data parameter to the method, and you're trying to create a data variable with the fixed statement. Perhaps the parameter is supposed to be m_data?

In any case, m_data.ToPointer() already gives a fixed address, so there's no need to fix it again. You should be able to write:

byte*** data = (byte***)(m_data.ToPointer());

It's unclear exactly what you're trying to do here. I'm fairly certain, though, that you want that pixel variable to be a byte rather than an int. Otherwise you're going to get an access exception when you try to write off the end of the bits data.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Thanks for the hint, it works. Probably, I was too tired at the end of the day. For the second part, I'm trying to access third dimension of bytes, since it represents rgb color (byte[i][j][0] - r, byte[i][j][1] - g, [i][j][2] - b). So, there's I have to convert byte array to int (bitmap pixel). – ilja Nov 19 '12 at 07:34