Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 356
Mason Wheeler Avatar asked Jul 20 '09 12:07

Mason Wheeler


People also ask

How do I use a PNG image?

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.


2 Answers

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; 
like image 63
Ondrej Kelle Avatar answered Oct 10 '22 09:10

Ondrej Kelle


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); 
like image 29
Cyril Leroux Avatar answered Oct 10 '22 10:10

Cyril Leroux