|TechTalks / VCL TGraphic basics|
AWare Systems on-line
What goes in a Delphi VCL TGraphic, TPicture, TImage, and how to convert between graphic file formats?
> What's in a Delphi VCL TGraphic, TPicture, TImage, anyway? When to use what?
There seems to be a lot of confusion about this. The actual intention of the VCL TGraphic design is poorly understood, design-wise corrupted by the non-fitting TBitmap, badly documented, and very often confused with the functionality of a TImage. It is nevertheless a very fine design.
In short (and not entirely absolutely correct):
(The above is not entirely correct, in that a TGraphic doesn't even know how to maintain and draw a raster, really. Even this functionality is covered in the descendant objects. That's good design because it allows for e.g. multipage or video TGraphic classes, and for tile array rasters that withstand the big image problem and such... But it confuses the point I tried to make, and there aren't that many multipage or video TGraphics around, so...)
A common application of this object oriented design is converting between graphic file formats. Suppose you need to convert a JPEG file to a BMP file, for example. This involves reading the JPEG file into a raster, and subsequently writing out that raster to a BMP file. The first part of this task is covered by the TJpegImage object, since this one knows how to read and decode a JPEG file. The raster object, managed by this TJpegImage, subsequently needs to be transfered to a TBitmap object. This TBitmap object, in turn, is able to encode and write out the BMP file.
Eliminating all exception handling and such, for clearity, this amounts to:
procedure ConvertJpegFileToBitmapFile(const SourceFilename, DestinationFilename: String);
jpeg := TJpegImage.Create;
bmp := TBitmap.Create;
Now you see where the Assign comes in. Internally, these TJpegImage and TBitmap objects are supposed to maintain rasters with usage counters. The Assign statement, officially, copies the raster content of the jpeg object into the bmp object. Underneath, this does *not* involve creating a second raster and copying the pixel data, but boils down to sharing the raster object and incrementing its usage counter, eliminating the need for duplicating memory.
Please drop us a line if you wish to be kept informed about changes and additions to this page.