ImageProcessorCore:尝试重新采样图像会产生零长度响应

时间:2016-07-26 23:05:59

标签: asp.net-core-mvc imageprocessor

我正在尝试将JPG图像从300dpi重新采样到150dpi,并且正在返回零长度文件。

Controller的ActionResult:

public ActionResult ViewImage(string file, int dpi = 300, bool log = true)
{
    FileExtensions fileExtensions = new FileExtensions();
    ImageExtensions imageExtensions = new ImageExtensions();

    FileModel fileModel = fileExtensions.GetFileModel(file);
    string contentType = fileModel.FileType;
    byte[] fileData = fileModel.FileData;
    string fileName = Path.GetFileNameWithoutExtension(fileModel.FileName) + "_" + dpi + "DPI" + Path.GetExtension(fileModel.FileName);
    FileStreamResult resampledImage = imageExtensions.ResampleImage(fileData, contentType, dpi);
    resampledImage.FileDownloadName = fileName;
    return resampledImage;
}

ResampleImage方法:

public FileStreamResult ResampleImage(byte[] fileData, string contentType, int targetDPI)
{
    MemoryStream outputStream = new MemoryStream();
    using (Stream sourceStream = new MemoryStream(fileData))
    { 
        Image image = new Image(sourceStream);
        image.HorizontalResolution = targetDPI;
        image.VerticalResolution = targetDPI;
        JpegEncoder jpegEncoder = new JpegEncoder();
        jpegEncoder.Quality = 100;
        image.Save(outputStream, jpegEncoder);
    }           
    FileStreamResult file = new FileStreamResult(outputStream, contentType);
    return file;
}

1 个答案:

答案 0 :(得分:1)

我认为我最好在这里回答,因为我们已经在问题跟踪器上处理了它。

ImageProcessorCore目前(2016-08-03)是alpha软件,因此尚未完成。当您遇到问题时,jpeg图像中无法设置水平和垂直分辨率。现在已经解决了。

顺便提一下,有些重载允许保存为jpeg而无需创建自己的JpegEncoder实例。

image.SaveAsJpeg(outputStream);