Load .png to TImage in Embarcadero C++Builder XE5

c++, c++builder-xe5, png, timage

Solution

Just add an include for `stdimage.hpp`.

At designtime, this will make .png files available in the `Image.Picture` dialog. At runtime, you can create and load a TPngImage with the file, and assign it to the `Image.Picture`.

#include <stdimage.hpp>

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
  TPngImage* img = new TPngImage();
  img->LoadFromFile("C:\\Images\\calculator.png");
  Image1->Picture->Assign(img);
  delete img;
}

(Delphi code and explanation included as well as C++ Builder, because the `TImage` and `TPngImage` are both Delphi classes and therefore it's relevant, and because C++ Builder users should be pretty familiar with translating Delphi code as the entire VCL is built on it. Also, as `TImage` is a Delphi VCL component, a Delphi user may find the question and find the information useful as well.)

procedure TForm4.Button1Click(Sender: TObject);
var
  Png: TPngImage;
begin
  Png := TPngImage.Create;
  try
    Png.LoadFromFile('C:\Images\calculator.png');
    Image1.Picture.Assign(Png);
  finally
    Png.Free;
  end;
end;

More info in the XE5 documentation

Problem

I need to load a .png image because i need it's transparency. It's not an option to convert it for example in bmp because i lose it's transparency. How can I do that?

Original source

Related problems