我正在尝试将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;
}
答案 0 :(得分:1)
我认为我最好在这里回答,因为我们已经在问题跟踪器上处理了它。
ImageProcessorCore目前(2016-08-03)是alpha软件,因此尚未完成。当您遇到问题时,jpeg图像中无法设置水平和垂直分辨率。现在已经解决了。
顺便提一下,有些重载允许保存为jpeg而无需创建自己的JpegEncoder
实例。
image.SaveAsJpeg(outputStream);