Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - convert bmp to png fails

Tags:

bitmap

png

delphi

I have the following simple code to convert 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;

While the conversion to bmp works and I can even open it in mspaint and see its content, the conversion to png fails and I have a blank png image. What am I doing wrong?

like image 209
Amos Avatar asked Dec 30 '25 05:12

Amos


1 Answers

You have not set the dimensions (height and width) of the PNG image object. You would need to do that before drawing to it.

Easier however would be a simple assignment:

png.Assign(Bitmap);
like image 53
David Heffernan Avatar answered Jan 02 '26 09:01

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!