Fast copy of Color32[] array to byte[] array

后端 未结 3 1709
深忆病人
深忆病人 2020-12-06 13:07

What would be a fast method to copy/convert an array of Color32[] values to a byte[] buffer? Color32 is

3条回答
  •  情话喂你
    2020-12-06 13:35

    With modern .NET, you can use spans for this:

    var bytes = MemoryMarshal.Cast(colors);
    

    This gives you a Span that covers the same data. The API is directly comparable to using vectors (byte[]), but it isn't actually a vector, and there is no copy: you are directly accessing the original data. It is like an unsafe pointer coercion, but: entirely safe.

    If you need it as a vector, ToArray and copy methods exist for that.

提交回复
热议问题