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?
How to open a PNG file. Nearly all built-in image editing programs can open PNG files. Whether you use a Mac or Windows computer, simply search for the file name and double-click on it. You can then choose the program you want to use from the list of options your computer gives you.
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;
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With