Skip to content

Can not change DPI of JPG image. #92

Description

@eat-sleep-code

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 :-( )

Activity

  1. tocsoft commented on Jan 26, 2017

    @tocsoft
    Member

    We haven't yet released ImageSharp to nuget.org so there's no chance your working with an official release of our code. (we haven't haven't even released a full 1.0.0 yet, we are still in prerelease atm, let alone 1.0.0.7).

    Please ensure your using our official myget feed https://www.myget.org/gallery/imagesharp for ImageSharp as I'm sure sure you will understand we can't support unofficial builds released by third parties.

    In the interim I will close this issue, please feel free to comment back once/if you reproduce your issue with our latest official build and we'll reopen so we will can investigate further for you.

  2. eat-sleep-code commented on Jan 26, 2017

    @eat-sleep-code
    Author

    Visual Studio was showing Latest prerelease 1.0.0-alpha7, which is apparently incorrect. I guess at some point someone changed the version naming structure. I will test with 1.0.0-alpha1-00071

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions