public static byte[] ToPixelData <T>(this Image image, TextureFormat fmt) where T : unmanaged, IPixel <T> { var img = image.CloneAs <T>(); var stride = fmt.Stride(img.Width); var bytes = new byte[fmt.Size(img.Width, img.Height)]; for (int i = 0; i < img.Height; i++) { MemoryMarshal.AsBytes(img.GetPixelRowSpan(i)) .CopyTo(bytes.AsSpan(stride * i, stride)); } return(bytes); }
public static int Size(this TextureFormat fmt, int width, int height) { return(fmt.Stride(width) * height); }