How do I make a PNG resource?
I've got a form with a large TImage
on it as a background. Problem is, this is stored directly in the DFM as a bitmap, which takes up about 3 MB. The original PNG file is ~250K. I'd like to try to reduce bloat by embedding the PNG in a resource, and then having the form load it during OnCreate
. I can do that now that Delphi 2009 includes PNG support, except I don't quite know how to build a resource file with a PNG in it. Anyone know how that's done?
Solution 1:
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;
Solution 2:
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);