Archive

Posts Tagged ‘Png’

Converting to D2009: How to keep PNGs in TImage

November 11th, 2008

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.

Arvid Delphi , ,

PngImageList Fix for DEP and Update for D2009

October 26th, 2008

Hi,

probably you already encountered the problem that your Delphi IDE crashes instantly when using the TPngImageList Thany made available years ago. This happens if you use the TPngImageList Designtime Package under systems with activated Data Execution Prevention (DEP).

Despite the fact that this problem existed for some years now Christian of the JEDI Windows API Team was the first to write about it in his blog recently.

Mitja P. added a comment which provides the source and a solution for the bug:

There is a function PatchPtr used in PngComponents unit PngImageList that needs a change of VirtualProtect to PAGE_EXECUTE_READWRITE instead of only PAGE_READWRITE if I remember correctly. I am also not sure if I reported this to the author at the time I was playing around with this components or not.

I’ve verified that this fix solves the issue for Delphi 2006 up to Delphi 2009. Thank you Mitja!

Additionally there is an updated version of the TPngImageList (created by Uwe Raabe) available at the CodeGear Repository:

PngComponents for Delphi 2009

As Delphi 2009 only integrates the PNG support into native ImageLists this updated component allows for using your already existing PngImageLists from projects prior to Delphi 2009 without any change to the images!

Please be aware that this version needs the mentioned fix as well, so update the source before installing the package.

Arvid Delphi , , ,