1 回答
TA贡献1856条经验 获得超5个赞
您不能使用,fileStream因为流需要可读可写。如果您首先将数据复制到内存流,则可以压缩该流中的图像。您的代码应更改为:
private byte[] ConvertImageToByteArray(IFormFile image)
{
byte[] result = null;
// filestream
using (var fileStream = image.OpenReadStream())
// memory stream
using (var memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0; // The position needs to be reset.
var before = memoryStream.Length;
ImageOptimizer optimizer = new ImageOptimizer();
optimizer.LosslessCompress(memoryStream);
var after = memoryStream.Length;
// convert to byte[]
result = memoryStream.ToArray();
}
return result;
}
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报