[ACCEPTED]-How do I make a PNG resource?-resources

Accepted answer
Score: 60

Example text file (named myres.rc):

MYPNG RCDATA mypng.png

Added 1 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;
Score: 3

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

In the ResourceTest.rc file

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

In 2 the ResourceTest.rh file

#ifndef ResourceTestRH
#define ResourceTestRH

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

#endif

In the ResourceTest.cpp 1 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);
Score: 1

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

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

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

Score: 1

When using Resource Hacker, PNG images are added with 'PNG' ResType rather 3 than common RT_RCDATA.

A TPngImage Class Helper gives 2 a simple solution for this issue :

Type
  TPngImageHelper = Class Helper For Vcl.Imaging.pngimage.TPngImage
    Procedure LoadFromRHResourceName(Instance: HInst; Const Name: String);
  End;

...

Procedure TPngImageHelper.LoadFromRHResourceName(Instance: HInst; Const Name: String);
Var
  rs: TResourceStream;
Begin
  rs := TResourceStream.Create(Instance, PChar(Name), 'PNG');
  Try
    LoadFromStream(rs);
  Finally
    rs.Free;
  End;
End;

With 1 a simple use:

var
  pngInfo: TPngImage;
begin
  pngInfo := TPngImage.Create;
  try
    pngInfo.LoadFromRHResourceName(HInstance, 'MY_IMAGE.PNG');
    Image1.Picture.Graphic:= pngInfo;
  finally
    pngInfo.Free;
  end;
end;

More Related questions