Last active
October 13, 2022 22:06
-
-
Save norberteder/745e382e3c2eae95b01a to your computer and use it in GitHub Desktop.
C#: Change DPI of Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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