Delphi - bmp to png conversion fails

I have the following simple code to convert a clipboard image to bmp and then to png:

if Clipboard.HasFormat(CF_PICTURE) then
begin
    bitmap := TBitmap.Create;
    png := TPNGImage.Create;
    try
        bitmap.Assign(Clipboard);
        bitmap.SaveToFile(ExtractFilePath(application.ExeName) + '\filename.bmp');
        png.Draw(bitmap.Canvas, Rect(0, 0, bitmap.Width, bitmap.Height));
        png.SaveToFile(extractfilepath(application.ExeName) + '\filename.png');
    finally
        bitmap.free;
        png.free;
    end;
end;

      

As long as the conversion to bmp is working and I can even open it in mspaint and see its contents, the conversion to png fails and I have an empty png image. What am I doing wrong?

+3


source to share


1 answer


You did not specify the dimensions (height and width) of the PNG image object. You will need to do this before drawing.

A simple assignment would be easier:



png.Assign(Bitmap);

      

+7


source







All Articles