Lazarus IDE: Importing images into the project

3

Lately I've migrated from Delphi XE6 to Lazarus IDE, and since I'm not used to the new platform I'd like to know how I can import images, videos and other resources into the project and use them (without the need to use a specific directory of the operating system).

Taking advantage of the same situation, it would be feasible to create a zip file and store or simply create a package in the project itself containing these features (and how could you be creating this package and reference the resources in the Pascal code) .

OBS: I want to use the images in a TImage object and set it in the Picture property.

Thank you in advance.

    
asked by anonymous 02.12.2014 / 04:33

1 answer

2

Here I leave a tutorial with the solution to my question (contains some adaptations I made to ease the process).

Tutorial

First Lazarus IDE has a tool named lazres.exe , located in the tools folder of Lazarus ( X: strong> \ Lazarus directory \ tools \ lazres.exe) - replace what is in bold with the Lazarus installation drive and directory.

After locating it I will be explaining briefly how LazRes works.

Introduction to LazRes

Its operation is by command lines and allows the creation of Lazarus Resources extension files - a kind of package creator allowing you to add all the resources you want (such as images, audios, videos, general files, etc.).

For the package creation two parameters for LazRes.exe , the LRS package name strong> and resources :

lazres.exe <nome_do_pacote.lrs> <arquivo1 arquivo2 arquivo3...>

Step 1: Preparing to create packages with a batch file

This step can be ignored if you know how to handle the command prompt and also lazres.exe

Copy and paste the code below into Notepad. You should save it in the same directory as the X: \ Lazarus directory \ tools) with the name of LRSBat.bat p>

LRSBat.bat :

@echo off
cls
pushd %~dp0
set l=^call lazres.exe 
%l%
echo.
set /p f=Nome do Pacote ^(.lrs^):
set f=%f:.lrs=%
set f=%f%.lrs
@echo.
set a=
set p=
@echo.
echo Arraste um arquivo de cada vez e pressione ENTER para adicionar mais.
echo Quando finalizar digite /f/ e pressione ENTER para gerar o pacote final.
:addArquivos
set /p "a=Arquivo: "
if "%a%"=="/f/" (goto:criarLRS)
if exist %a% (set p=%p% %a% )

goto:addArquivos

:abort
exit

:criarLRS
echo ---------------------------------
%l% %f% %p%
echo.
echo ---------------------------------
ECHO Fim.
pause>nul
exit

Step 2: Creating your first package (using LRSBat.bat)

Run the LRSBat.bat as an administrator and follow the on-screen procedures. If successful, an LRS file will be created in the same directory with the chosen name.

Step 3: Implementing the LRS file (package) in the Lazarus project

Copy and paste the LRS file into your project folder.

With the project open in Lazarus and the code editor of the chosen unit , you are incrementing the following lines:

uses ..., LResources;

. . .

{ Ao final do projeto antes do end. incluir as linhas abaixo }
initialization
 {$I nomedopacote.lrs}

end.

This will allow you to compile the package_name.lrs package together with the project executable.

Step 4: Using an LRS Package File in the Project

For example, to add a package image file to a Lazarus image component, follow the procedures below. Suppose the image component is componenteDeImagem and nome_do_arquivo refers to the image name of the package (without the extension):

componenteDeImagem.Picture.LoadFromLazarusResource('nome_do_arquivo');

Regardless of the component being worked on, if you want to use a package file in the project you should use the LoadFromLazarusResource method.

References

link

    
06.12.2014 / 20:49