How do I make a PNG resource?

For those who use C++ Builder this code works for me :

In the ResourceTest.rc file

IMG_BMP BITMAP "Ressources\\myimage.bmp";
IMG_PNG RCDATA "Ressources\\myimage.png";

In the ResourceTest.rh file

#ifndef ResourceTestRH
#define ResourceTestRH

#define IMG_BMP "IMG_BMP"
#define IMG_PNG "IMG_PNG"

#endif

In the ResourceTest.cpp file

#include "pngimage.hpp"

// Loading bmp image from resource
Graphics::TBitmap *bmpImage = new Graphics::TBitmap();
bmpImage->LoadFromResourceName((int)HInstance, IMG_BMP);

// Loading png image from resource
TPngImage *pngImage = new TPngImage();
pngImage->LoadFromResourceName((int)HInstance, IMG_PNG);

If you're using Delphi 2009, TImage should store your PNG file as a PNG into the DFM file. The DFM will be larger because the binary content of the Picture.Data property of the TImage object is encoded in the DFM as hexadecimal text. But when the DFM is compiled into your EXE, it is compiled into a binary resource. Your image should then take up the same space inside the form's RCDATA resource as storing the PNG in its own RCDATA resource would.

I just tested this by opening one of my own Delphi 2009 DFM files that have a TImage component with a PNG image loaded at design time in a text editor, copying the contents of the Picture.Data property and pasting them into a hex editor. The hex editor shows me that the Picture.Data property stores an actual PNG file prefixed with 10 bytes. The first byte is $09 and the next 9 bytes spell TPngImage. If I delete those 10 bytes and save the file in the hex editor, I get a proper PNG file.

So if you're using Delphi 2009, simply load the PNG image into a TImage component at design time.


Example text file (named myres.rc):

MYPNG RCDATA mypng.png

Added to project:

{$R 'myres.res' 'myres.rc'}

Example of loading at runtime:

uses
  PngImage;

var
  Png: TPngImage;
begin
  Png := TPngImage.Create;
  try
    Png.LoadFromResourceName(HInstance, 'MYPNG');
    Image1.Picture.Graphic := Png; // Image1: TImage on the form
  finally
    Png.Free;
  end;
end;

pretty similar to my example: http://www.ciuly.com/index.php/delphi/article/delphi/7-multimedia/10-pngfromresource.html