0
votes

I'm a newbie in programming and I have a game(like Flappy Bird). IN my project I have to change Image usually and so in my MainForm I have an TImage. *My question is HOW CAN I EXACTLY KNOW WHERE THE IMAGE'S PATH of course I use Image1.Picture.LoadFromFile

ex : I have - a picture's name : 'Scene1.bmp'; - a TForm1.Button1Click(Sender: TObject);

*Mission : When user click on Button, the Image will 'LoadFromFile' to set the Scene1.bmp to TImage

Please help me, Thank alot!

2
You write your installer program to copy the file to a ProgramData subfolder. Search SO or MSDN for details about this. F.ex. thisTom Brunberg
I still feel vague, Can you get more clearer?Johnny
You (or your installer program) should decide where your images are stored. Use a clearly defined location that can be queried using the Windows API, for instance a subdirectory (one your installer creates) of the user's ProgramData folder. Take a look at ShGetFolderPath or similar functions to find out where that is. You can also store them in the program directory.Rudy Velthuis
If you don't know where it is, what chance do we have?David Heffernan
Where you've last seen that your image file Scene1.bmp?Free Consulting

2 Answers

0
votes

You could use the folder (or a subfolder) of your application's EXE file to store your Scene1.bmp file.

In the VCL you have the global object Application of type TApplication. You can use the property Application.ExeName to get the file name of the application's executable file including path information. See the documentation.

With TPath.GetDirectoryName you can then extract drive and directory parts. See this documentation.

So for example the path of the subfolder named Sub is TPath.GetDirectoryName(Application.ExeName)+'\Sub';

Like this you can construct the complete path and file name and use it in LoadFromFile.

0
votes

When you address a file in your application just with File-Name (like 'Scene1.bmp') your application will search for the file on the path of your application exe file as Default-path of your application, but Default-Path may be changed for some reasons, you can set the Default-Path of your application with SetCurrentDir function and get Default-Path with GetCurrentDir function

Every Process has a "Current Directory" as Default-Path and it is changeable, so you should always work with full address of the file you want to use

You can get Full-Path of exe file of your application from ExeName field of Application Class :

var
 MyApplicationFullPath : String;
begin
 MyApplicationFullPath := Application.ExeName;
end;

With ExtractFilePath function you can get the Path without File-Name of your application exe file, in fact address of folder of exe file and then you can use this address to work with files are besides of your exe file, for example :

 Image1.Picture.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Scene1.bmp')

You can be sure that 'Scene1.bmp' will be found and Default-Path of your application doesn't matter

HOW CAN I EXACTLY KNOW WHERE THE IMAGE'S PATH

Application's Current Directory + 'Scene1.bmp' :

ImageFullAddress := GetCurrentDir + 'Scene1.bmp'