我想将以下代码更改为一个高效的 Web API 控制器方法,该方法可以返回 30 到 50 个平均 3MB 大小的 JPEG(对于 <20 个同时用户)。
public async Task<HttpResponseMessage> Get(int imageid) {
return await Task.Run(() => {
var dzImage = _dataContext.DeepZoomImages.SingleOrDefault(_ => _.ImageID == imageid);
if (dzImage == default(DeepZoomImage)) {
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
var response = new HttpResponseMessage(HttpStatusCode.OK);
var bitmap = GetFullSizeBitmap(dzImage);
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Jpeg);
response.Content = new ByteArrayContent(memoryStream.ToArray());
var fileName = string.Format("{0}.jpg", dzImage.ImageName);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName };
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
memoryStream.Flush();
return response;
});
}
上述方法处理一张图像。对于这种使用/加载场景,我怎样才能最好地对其进行复数化以有效地处理多个图像?是否可以返回 Task<HttpResponseMessage>[] 或 Task<HttpResponseMessage[]>?
或者生成所有图像并一次性返回它们会更好吗?
谢谢你的帮助...