Delphi - JPEG Images

The standard TImage component can display jpeg images without any changes to your code IF you add jpeg to the uses clause. In a sample program, simply adding jpeg increased the exe size from 372K to 454K. Uh, if any included class definition has jpeg, then every TGraphic component has the capability.

Overview | Manipulating Pixels | Save bitmap as JPEG file | Bogus JPEG Error #36


Overview

TGraphic maintains a list of the supported graphics formats. When jpeg is added to a uses clause, it adds itself to the list. Because of how this works, it does not matter which source file contains the uses clause - if any file includes the jpeg unit, then every TGraphic will support it.

This is implemented via RegisterFileFormat which registers a new TGraphic class for use in LoadFromFile. You can then use the appropriate files with code similar to

GraphicFilter(TGraphic) produces the following picklist format I was not able to find any other way to determine which file types are supported. The list used to store the registered types is private to Graphics.pas.


Manipulating Pixels

You can not directly manipulate (read or write) jpeg pixels. Instead, you must first copy the jpeg to a bitmap and then access the bitmap's pixels via its canvas. This code creates a TList of bitmaps made from jpegs. This code fragment reads the bitmaps from the list and plots pixel values. Reference: Converting a BMP to a JPEG and vice-versa under Delphi 3 - by Borland Developer Support Staff


Save bitmap as JPEG file

When reading a file, the TGraphic object inspects the file extension and creates the appropriate object type. Unfortunately, when writing a file, the extension is ignored. As a result, your code will have to do all the work.

The following example is strongly based on the help example for TJPEGImage.Assign (same as example for TGraphic.SaveToFile).

I hard coded the CompressionQuality at 100% because I am using this to document screen images and I don't want any compression artifacts.


Bogus JPEG Error #36

Delphi provides jpeg support via the jpeg unit (jpeg.dcu) which is simply a wrapper for a library provided by the Independent JPEG Group (IJG).

When writing JPEG files larger than 1 mb, you may get the bogus

and a zero byte file.

One fix is to modify JPEG.pas and recompile. With Delphi 5 professional, the source code is located on the CD at

The problem is fixed by adding a try..except block and recompiling.

Note that jpeg_finish_compress is implemented in the library (it is external) and not written in Delphi.

Though several web pages say different, this problem was observed in Delphi 6.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / Delphi / JPEG_Images.html