I have this struct definition:
public struct ChangedByte
{
public byte R;
public byte G;
public byte B;
public int x;
public int y;
}
I have created a:
List<ChangedByte> testme = new List<ChangedByte>();
(and added items to it)
I convert this to an Array:
ChangedByte[] alpha = testme.ToArray();
I have this function I found with a similar question on SO:
byte[] StructureToByteArray(object obj)
{
int len = Marshal.SizeOf(obj);
byte[] arr = new byte[len];
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(obj, ptr, true);
Marshal.Copy(ptr, arr, 0, len);
Marshal.FreeHGlobal(ptr);
return arr;
}
I call it like this:
byte[] test = StructureToByteArray(alpha);
I get the error:
Type 'new_encoder.Form1+ChangedByte[]' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.
I would like to convert a list of objects into a byte array (and possibly avoid serialization as it tends to bulk up the size of the array)
Can it be done?
ADDITIONAL:
I have tried amending the Struct to this:
public struct ChangedByte
{
[MarshalAs(UnmanagedType.LPWStr)]
public byte R;
[MarshalAs(UnmanagedType.LPWStr)]
public byte G;
[MarshalAs(UnmanagedType.LPWStr)]
public byte B;
[MarshalAs(UnmanagedType.LPWStr)]
public int x;
[MarshalAs(UnmanagedType.LPWStr)]
public int y;
}
but still the same error.