且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像转换为字节数组的最快方法

更新时间:2021-12-05 09:10:07

那么有没有其他方法可以实现这个目标?

So is there any other method to achieve this goal?

没有.为了将图像转换为字节数组,您必须指定图像格式 - 就像将文本转换为字节数组时必须指定编码一样.

No. In order to convert an image to a byte array you have to specify an image format - just as you have to specify an encoding when you convert text to a byte array.

如果您担心压缩伪影,请选择无损格式.如果您担心 CPU 资源,请选择一种无需压缩的格式 - 例如,只需原始 ARGB 像素.但这当然会导致更大的字节数组.

If you're worried about compression artefacts, pick a lossless format. If you're worried about CPU resources, pick a format which doesn't bother compressing - just raw ARGB pixels, for example. But of course that will lead to a larger byte array.

请注意,如果您选择了一种确实包含压缩的格式,那么在此之后压缩字节数组是没有意义的 - 几乎可以肯定没有任何有益效果.

Note that if you pick a format which does include compression, there's no point in then compressing the byte array afterwards - it's almost certain to have no beneficial effect.