TPNGObject provides
flexibility by allowing to convert from a Bitmap file format to
Portable Network Graphics format.
This is easily done like in the same way as any graphic class in delphi.
IMPORTANT: Always remember to add pngimage to the unit uses.
Converting from Windows bitmap file to PNG file
This method loads a bitmap and saves it using png format
procedure
BitmapFileToPNG(const Source, Dest: String);
var
Bitmap: TBitmap;
PNG: TPNGObject;
begin
Bitmap := TBitmap.Create;
PNG := TPNGObject.Create;
{In case something goes wrong, free booth
Bitmap and PNG}
try
Bitmap.LoadFromFile(Source);
PNG.Assign(Bitmap);
//Convert data into png
PNG.SaveToFile(Dest);
finally
Bitmap.Free;
PNG.Free;
end
end;
Converting from PNG file to Windows bitmap file
The above inverse. Loads a png and saves into a bitmap
procedure
PNGFileToBitmap(const Source, Dest: String);
var
Bitmap: TBitmap;
PNG: TPNGObject;
begin
PNG := TPNGObject.Create;
Bitmap := TBitmap.Create;
{In case something goes wrong, free booth PNG
and Bitmap}
try
PNG.LoadFromFile(Source);
Bitmap.Assign(PNG);
//Convert data into bitmap
Bitmap.SaveToFile(Dest);
finally
PNG.Free;
Bitmap.Free;
end
end;
Converting from TImage to PNG file
This method converts from TImage to PNG. It has full exception
handling and allows
converting from file formats other than TBitmap (since they allow
assigning to a TBitmap)
procedure
TImageToPNG(Source: TImage; const Dest: String);
var
PNG: TPNGObject;
BMP: TBitmap;
begin
PNG := TPNGObject.Create;
{In case something goes wrong, free PNG}
try
//If the TImage contains a TBitmap,
just assign from it
if Source.Picture.Graphic is TBitmap then
PNG.Assign(TBitmap(Source.Picture.Graphic))
//Convert bitmap data into png
else begin
//Otherwise try to
assign first to a TBimap
BMP := TBitmap.Create;
try
BMP.Assign(Source.Picture.Graphic);
PNG.Assign(BMP);
finally
BMP.Free;
end;
end;
//Save to PNG format
PNG.SaveToFile(Dest);
finally
PNG.Free;
end
end;