Including JPGs in the EXE of your application  

Send By: Radikal
Web : http://www.q3.nu
Email: radikal@q3.nu
Date: 27/05/00

Tip accessed 568 times

 


Maybe you need to use several JPGs in your application.
Instead of leaving them outside of your executable, with this trick you will be able to include it.

  • First, we will build ourselves a resource file where we will put the JPG files

    To make it it follows the steps that I here put you:



    Making our .RC file


  • Open a text editor (Notepad, by example) and write:


     1 RCDATA  UnJpg.JPG
     2 RCDATA  OtroJpg.JPG
    



  • Save the file with RC extenxión, for example: Imagenes.RC


    Compiling the .RC file


    Now we should compile it, to create a file of resources (.RES), for that which, the best thing is that you put in the same directory as much the JPGs as the file .RC that you have created.
    To compile the file, we will use the Delphi compiler: the BRC32 (Borland resource Compiler) that you will have in the subdirectory /BIN inside the directory where you have installed the Delphi.
    We will use:


     BRC32.EXE -r -v Imagenes.RC
    



    If everything has gone well, we will have generated the file Imagenes.RES


    Including the .RES file in your application


    Now, you should include in your application, the file .RES that you have created. You will make it including this line in your form, in the implementation part:


     implementation
    
     {$R *.DFM}
     {$R Imagenes.RES}
    




    Showing these JPGs


    It was time to show the JPGs:


    Los pasos son los siguientes:


  • Create a temporal TJpegImage (remember add 'JPeg' in the uses)
  • Create a TResourceStream pointing to the JPG to load (in the example the #1)
  • Load the JPG in the temporal TJPegImage
  • Draw the temporal TJPegImage in a TImage of our form
  • Free the temporals.

    The code:


     procedure TfSplash.FormCreate(Sender: TObject);
     var
        Corriente   : TResourceStream;
        Imagen      : TJPegImage;
     begin
       Imagen:=TJPegImage.Create;
       Corriente:=TResourceStream.Create( hInstance,
                                          '#1',
                                          RT_RCDATA );
       Corriente.Seek(0,soFromBeginning);
       Imagen.LoadFromStream(Corriente);
       Image1.Canvas.Draw(0,0,Imagen);
       Imagen.Free;
       Corriente.Free;
     end;