I am running 1.0.0.7 -- which appears to be the latest version according to Nuget -- and having an issue with changing the DPI of JPEG images.
I have the following code:
public byte[] GetImage(Stream sourceStream, string contentType, double targetDPI)
{
MemoryStream outputStream = new MemoryStream();
try
{
using (sourceStream)
{
Image image = new Image(sourceStream);
double currentDPI = image.HorizontalResolution;
double resizeRatio = targetDPI / currentDPI;
int targetWidth = (int)Math.Round((image.Width * resizeRatio));
int targetHeight = (int)Math.Round((image.Height * resizeRatio));
image.HorizontalResolution = targetDPI;
image.VerticalResolution = targetDPI;
BicubicResampler resampler = new BicubicResampler();
switch (contentType)
{
case "image/gif":
GifEncoder gifEncoder = new GifEncoder();
gifEncoder.Quality = 256;
image.Resize(targetWidth, targetHeight, resampler, false).Save(outputStream, gifEncoder);
break;
case "image/jpeg":
JpegEncoder jpegEncoder = new JpegEncoder();
jpegEncoder.Quality = 100;
image.Resize(targetWidth, targetHeight, resampler, false).Save(outputStream, jpegEncoder);
break;
default:
PngEncoder pngEncoder = new PngEncoder();
image.Resize(targetWidth, targetHeight, resampler, false).Save(outputStream, pngEncoder);
break;
}
}
}
catch (Exception ex)
{
new LoggerExtensions().LogException(ex, "Brand Central Station: Image Extensions");
return new byte[0];
}
return outputStream.ToArray();
}
If I pass a 300dpi PNG image and set the targetDPI variable to 72dpi, the file is output at 72dpi (yeah, it works!)
If I pass a 300dpi JPG image and set the targetDPI variable to 72dpi, the file is output at 300dpi (boo, it doesn't work :-( )
I am running 1.0.0.7 -- which appears to be the latest version according to Nuget -- and having an issue with changing the DPI of JPEG images.
I have the following code:
If I pass a 300dpi PNG image and set the targetDPI variable to 72dpi, the file is output at 72dpi (yeah, it works!)
If I pass a 300dpi JPG image and set the targetDPI variable to 72dpi, the file is output at 300dpi (boo, it doesn't work :-( )