简体   繁体   中英

access unmanaged array inside delphi dll from c#

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM