Monday, March 30, 2009

Converting to D2009: How to keep PNGs in TImage

Hi,


if you have been using Gustavo Daud’s PNGImage before and consider migrating existing projects to D2009 you must keep in mind that you will loose all previously assigned PNGs in your TImages.


As you all know Delphi 2009 ships with native PNG support but there are some differences. In PngImage.pas CodeGear declared



type
TPNGObject = TPngImage deprecated 'Use TPngImage.';

So the classname for PNGs changed and the old one is marked as deprecated.


The problem: All TImage pictures are stored within the form’s .DFM file. Before inserting the binary data the IDE stores the classname (and it’s length) as a prefix to the data.


When using Gustavo’s component in D2006 it will store 0A54504E474F626A656374 which is in ASCII TPNGObject (the 1st 0A is the length of the following classname). In Delphi 2009 it stores 0954506E67496D616765 which is in ASCII TPngImage (again 09 is the length).


So when opening an existing form in D2009 the IDE searches for TPNGObject classname and as it cannot find it anymore the images are emptied.


With the help of Andreas Hausladen I came up with a solution for this:



PngClassWrapper.pas

{$IFDEF VER200}
uses
Classes, Graphics, PngImage;

type
TPngObject = class(TPngImage);
{$ENDIF}

implementation

{$IFDEF VER200}
initialization
TPicture.RegisterFileFormat('PNG', 'Portable Network Graphics (Compatibility Wrapper)', TPngObject);
finalization
TPicture.UnregisterGraphicClass(TPngObject);
{$ENDIF}

After discussing the issue Uwe Raabe yesterday published a fix inside of the PngComponents for Delphi 2009.


He used:



type
TPNGObject = class(TPngImage);

begin
TPicture.RegisterFileFormat('', '', TPNGObject);
end.

The empty strings will help to hide the wrapper from the FileOpen Dialog within the filter selection in the IDE.


An updated version if his port of the PngComponents for D2009 is available at CG:

PngComponents for Delphi 2009


Btw: The PngComponents Uwe published is still compatible with older Delphi Versions (tested with Delphi 2006). Only the PngImage Gustavo previously released on SourceForge is not available anymore as the license has been revoked as D2009 now ships with an enhanced version of it.

No comments: