I have a IntPtr
called rawbits, which points to a 10MB array of data, 16 bit values. I need to return a managed ushort
array from that. The following code works but there is an extra BlockCopy
I would like to get rid of. Marshal.Copy
does not support ushort
. What can I do? (FYI: the rawbits is filled in by a video framegrabber card into unmanaged memory)
public const int width = 2056;
public const int height = 2048;
public const int depth = 2;
public System.IntPtr rawbits;
public ushort[] bits()
{
ushort[] output = new ushort[width * height];
short[] temp = new short[width * height];
Marshal.Copy(rawbits, temp, 0, width * height);
System.Buffer.BlockCopy(temp, 0, output, 0, width * height * depth);
return output;
}
The suggestions given in the following question did not help. (compiler error).
C# Marshal.Copy Intptr to 16 bit managed unsigned integer array
[BTW, the short array does have unsigned 16 bit data in it. The Marshal.Copy()
does not respect the sign, and that is what I want. But I would rather not just pretend that the short[]
is a ushort[]
]