Changing the image format in ASP with csImageFile

It is easy to use csImageFile to change the format of an image from one supported type to another. Simply load the image in the orignal format and then save or export using the new format.

The following code fragment loads a TIFF image and saves it as a JPEG.

Image.ReadFile "c:\images\photo.tif"
Image.WriteFile "c:\images\photo.jpg"

The next fragment loads the TIF and streams the image to the browser as a JPG. This is a common conversion because it allows a TIFF to be displayed in browsers that do not support the format.

Image.ReadFile "c:\images\photo.tif"
Response.ContentType = "image/jpeg"
Response.BinaryWrite Image.JPGData

The above code assumes that an instance of csImageFile has been created called "Image". When reading and writing files always use the full physical path and give the Internet Guest User account appropriate permissions to read, write or modify.

Image format conversions can sometimes lose information. For example converting PNG to JPG can lose transparency, and converting JPG to GIF can lose pixel density (DPI / Pixels per metre) as well as colour depth.

The first example can be used in a loop as part of a batch conversion. The FileList collection can be useful if a complete folder of files needs to be converted. For more details, refer to the csImageFile instructions.

File formats supported by csImageFile are BMP, GIF, JPG, PNG, PSD, PCX, TIF and WBMP. Images can be exported to PDF, but PDFs cannot be read.


This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.