Reading and changing the DPI of an image in ASP with csImageFile

The pixel density of an image can be read or set with csImageFile. The property is DPI and it is always measured in dots per inch. The functions DPMToDPI and DPIToDPM are available for converting to and from metric. The DPI property applies to both the X and Y pixel densities so it is not possible to save an image from csImageFile that has different values for X and Y pixel density. Also, csImageFile will always save the pixel density as dots per inch even if the original image used metric units.

The following code fragment loads an image and displays the pixel density.

Image.ReadFile "c:\images\photo.jpg"
Response.Write Image.DPI

The next fragment sets the DPI to 100

Image.ReadFile "c:\images\old.jpg"
Image.DPI = 100
Image.WriteFile "c:\images\new.jpg"

Not all formats store a pixel density value and these include GIF and WBMP. Some image editing software will show a value for DPI when opening one of these images but they are only selecting a default. The GIF and WBMP file formats do not have anywhere to store this property. csImageFile can read and write pixel density in PNG images but this is an extension to the format that not all software supports.

When an image contains the Exif attributes XResolution and YResolution these will be modified when the DPI property is changed. This was new behaviour in version 5 of csImageFile and it resolved a conflict with Adobe Photoshop 7 or later. Photoshop 7 reads the DPI of a JPEG from the Exif attributes instead of from the JPEG itself. With older versions of csImageFile it was necessary to call ExifClear to remove the Exif attributes completely in order to get Photoshop to read the correct DPI setting.