Skip to content

Instantly share code, notes, and snippets.

@norberteder
Last active October 13, 2022 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norberteder/745e382e3c2eae95b01a to your computer and use it in GitHub Desktop.
Save norberteder/745e382e3c2eae95b01a to your computer and use it in GitHub Desktop.
C#: Change DPI of Image
var filename = @"--input--image--";
var targetfile = @"--output-image--";
using (var image = Image.FromFile(filename))
{
// changing to 96 DPI
var resolution = new byte[8] { 0, 0, 96, 0, 0, 0, 1, 0 };
if (image.PropertyIdList.Contains(282))
{
var xResolution = image.GetPropertyItem(282);
xResolution.Value = resolution;
image.SetPropertyItem(xResolution);
}
if (image.PropertyIdList.Contains(283))
{
var yResolution = image.GetPropertyItem(283);
yResolution.Value = resolution;
image.SetPropertyItem(yResolution);
}
image.Save(targetfile, image.RawFormat);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment