Delphi - PNG Images
For web pages, images are typically in either jpeg or png format.
The image editor supplied with Delphi 5 does not supply either of those.
Windows Paint
Start / Programs / Accessories / Paint
%SystemRoot%\system32\mspaint.exe
|
provides both. I eventually got tired of using Windows Paint
to document my programs .. so I wrote
my own screen capture utility.
Delphi 5 provides a unit to create jpeg files, but not png.
(More recent versions contain png support.)
This page discusses using
in Delphi 5 to provide png support.
Overview
| Save PNG File
| A Test Case
Overview
Several forums suggest using TPNGImage by Gustavo Daud
but claim that it is no longer available on sourceforge.
It turns out that as of June 2014, they are correct.
Luckily, I found part of it available on Torry's Delphi page.
- torry.net - indicates that the code is FWS - Free with source code
- sourceforge - a page, but no files
The comments indicate that it works with D2005 D2006 D2007 D6 D7,
but not Delphi 5 (D5) .. so I took a chance.
The zip
contains source code, a few object files, and a help file.
The help file says that there is one package file per Delphi version .. and to use the correct one.
Well, there aren't any package files!
So I created one.
- Create a new package
File / New... / New tab / Package
|
- Be sure to name the package (save it) before compiling it!!!
- Add all 4 source files in pngimage.zip
- Press Compile
- There will be several warnings - I just ignored them
- Press Install
That's all there is to it. There will be a new "button" added to the Samples tab
- but it just has a default icon since there was no resource file in the zip.
(The code example in the next section does not use this.)
Save PNG File
This is the code I used - no big deal.
uses pngimage;
procedure Save_bitmap_as_png_file_mc(bitmap: TBitmap; FileName: string);
procedure Save_bitmap_as_png_file_mc(bitmap: TBitmap; FileName: string);
var
png: TPNGObject; //Requires the "pngImage" unit added to "uses" clause.
ext : string;
begin
ext := ExtractFileExt(FileName) ;
if SameText(ext, '.png') then begin
// based on code from help - pngimage\PngDelphi.chm
png := TPNGObject.Create;
try
png.Assign(Bitmap);
png.SaveToFile(FileName);
finally
png.Free;
end;
end;
end;
|
It works using Delphi 5 on Windows XP.
A Test Case
Using a single image, these are the file sizes
jpeg | Delphi TJpegImage at max quality | 138 kb
|
---|
png | Delphi TPNGObject | 18 kb
|
---|
MS Paint | 23 kb
|
I was not able to detect any visual difference between the files.
Author:
Robert Clemenzi