Change TImage picture at runtime

Hi experts,

When you add an image to the TImage component and compile it the image stays inside the exe.

I need to be able to change the image on the TImage component at runtime and keeping it inside the exe.

How can i do this please???

Thanks

ST3VO
ST3VOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

diniludCommented:
Image1.Picture.LoadFromFile(FilePath)
0
diniludCommented:
Image1.Picture.Assign(Image2.Picture)
0
ST3VOAuthor Commented:
So...if I use: Image1.Picture.Assign(Image2.Picture) and load the picture....can I then erase the picture and it will still stay?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ST3VOAuthor Commented:
I mean...Image1.Picture.LoadFromFile(FilePath) .... don't want to assign from 1 Timage to another.

Just load it and then be able to delete the picture.

0
diniludCommented:
assign the old picture to a picture variable.
0
ST3VOAuthor Commented:
But the picture must be loaded into the TImage  and then stay!

Like

procedure oncreate ....
begin
  if fileexists(FilePath) then
  image1.Picture.LoadFromFile(FilePath);
end;

Then I can close the application and delete the image and it will show up as it's inside the exe.

That's what I need.



0
FactorBCommented:
1. Have two images one visible one invisible
Image1.Visible=True;
Image2.Visible=False;
Image1.Picture.Bitmap=Image2.Picture.Bitmap; ~@dinilud

2. Load it from file @dinilud

3. Make database with two columns, graphic type of field and text.
Fill the graphic field with different images and write some short description in the text field.
Make select query that will select certain column from the database depending on the text field.
Populate Image from DBImage or as a first choice use DBImage.
0
developmentguruPresidentCommented:
 What you are asking for is a way to overwrite the image within your EXE.  I have to point out that there are much simpler ways to achieve what you are looking for.  If your program were to save the image to a separate bitmap file then you would load it at startup.  When you change your image you would simply need to load the image into your control and then save it to the same file that is loaded when the program starts up.

  If you want to dynamically rewrite your EXE file with new resource information, it can be done but it is a mjor headache.  If you want a fast, easy to implemement, solution just use a separate file.
0
ST3VOAuthor Commented:
<quote>What you are asking for is a way to overwrite the image within your EXE. </quote>

Yes....that is exactly what I need to do!

0
developmentguruPresidentCommented:
As I understand it, resource editors are able to do that type of manipulation so I know it is possible.  The big issue here is that you cannot modify a file while it is in use.  The first test you would need to do is to see if you can copy another version of your EXE over the currently running EXE.  Since it is the same application that is running it may allow it.  If you can do that then you need to find out about the file format of the EXE.  Here is a good link to get you started.

http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx

Once you know how to use this information to create a copy of your EXE that includes the modified resource information all you need to do is replace the original.  If this type of operation is not done flawlessly it could easily lead to system crashes, etc.  If you still want to take the hard approach, I will wish you luck.
0
ST3VOAuthor Commented:
I've been searching EE and there are some samples of how to make a copy of your own exe etc...There is a demo which adds text into a memo inside an exe too....

But I have tried to work around samples available and cannot work it out so I decided to post the question here. So, yes I know the theory, it's just that I don't know how to do it :o/

0
ziolkoCommented:
you can do it using resources

FindResource and LoadResource functions to locate and load the resource.
LockResource function to retrieve a pointer to the resource data.
BeginUpdateResource()
UpdateResource()
EndUpdateResource()

should work with icons(RT_ICON) and bitmaps(RT_BITMAP), if you want to use other formats you need to use raw data(RT_RCDATA) but as developmentguru posted that's a lot of trouble.

ziolko.
0
developmentguruPresidentCommented:
You will need to decide how much time and effort you want to put into this.  Using a separate file can be done in minutes and is far easier as far as implementing other file types.  Trying the other approach will take far longer.  I wish I had better news for you.
0
FactorBCommented:
Changing executable file sounds too risky for me. Especially in a case you are developing commercial application. What if the whole process got interrupted somewhere in the middle and renders your application into useless footprint on the hard drive (I hope I am wrong about this). I would prefer database or folder for many pictures or single file for one image. If in a similar situation database get corrupted most of the times you can repair it from the executable with or without data loss. I am sorry that I am trying to persuade you to use another approach, but the whole process sounds like a good challenge and bad commercial solution.
0
Martin BarredaLic. in Computer ScienceCommented:
I will take developmentguru last advice, as i use something like that.
My apps, can "change" main form background image reading a file that is in same folder as the exe.
You achieve two solutions with this: avoid taking the risk of modifyng the exe, and not increase with a real reason the size of your app´s exe.
Just for "security", y change the image name to something like this APPNAME.xBK, meaning... that i do not have to know the name of the original image file... and prevent unknown users to modify, try, see, the image in a simple way.
That works for me for a long time in various customers, with a lot of different kind of users.
Hope this can help you as other previous comments may do!!!
0
ST3VOAuthor Commented:
I understand what you guys mean but I wouldn't have asked the question if I wanted to load an external image in that way as I know how to do that already. :o/
0
Martin BarredaLic. in Computer ScienceCommented:
I do not have my working code at hand... and is not something that i use, but i think is something like this:
Image1.Picture.LoadFromFile(fullpathandnameoffile);
0
ST3VOAuthor Commented:
I don't want to just load it....I need it to once loaded to stay inside the exe without the image having to be there anymore.
0
ziolkoCommented:
I did quick research, if you place TImage on your form and load image in designtime then build exe image data is stored in resources, the problem is it's not in separate resource but within dfm.

so resource type is RC_DATA resource name is class name of your form, for example TFORM1

and it makes it even more complicated, because you have to load dfm resource and find image in it.

You sure still want to do this?:)

ziolko.
0
ST3VOAuthor Commented:
I just need to find a way to be able to change the image on an exe and not have to distribute the image with the exe. :o/

0
ziolkoCommented:
>>I just need to find a way to be able to change the image on an exe and not have to distribute the image with the exe. :o/


what I understand from previus posts is that, you want that user can pick some picture load it into TImage so next time he starts exe he has his own picture but not loaded from disk.


btw. I'm already able to find image data within resource:)

ziolko.
0
ST3VOAuthor Commented:
[Quote]btw. I'm already able to find image data within resource:)
[/Quote]

Hmmm...interesting :o)
0
ziolkoCommented:
good news I got it working, bad news I can't make it update itself, so probably to update exe you'll need to call external exe which will alter and re-run target application.

unfortunatelly I caf finish it tomorrow

ziolko.
0
Steven-FernandezCommented:
Here is a solution for your problem.

Download this unit and just paste it in your project directory. (http://www.geocities.com/gacarpenter386/Latest.zip)

Here is a demo that uses the above unit and there is a jpg example available too: (http://www.geocities.com/gacarpenter386/MediaInExe.zip)

Don't forget to add ExeMod to the uses section.

To create a copy of your exe you just need to use:

Exe2File('MyNewExe.exe');
Exe := '';

See more code below:

Note: This is just a working example of how you can do this. If you need extra security just change the type of encryption.
          Also check the unit as it has usage examples and it is very easy to use.

If you have any other problems or questions just ask.


To insert the image into your application:
 
procedure TForm1.AddBMP;
var
Temp: String;
Path: String;
begin
Path:= ExtractFilePath(Application.ExeName);
//OpenDialog1.Execute;
If Pos('.BM',UpperCase(Path+'\Your.bmp')) <> 0 then
begin
  Temp := File2String(Path+'\your.bmp');
  Temp := DoXOR(Temp);//scramble it!
  InsOrReplaceInExe('BMP2',Temp);//name image as 'bmp1' -could be any name
  
  AlterExe;
end
else
  ShowMessage('File does not look like a BMP!'+chr(13)+'Operation ABORTED');
end;
 
//==============================================================================
 
procedure TForm1.ViewTheImage;
var
Temp: String;
MyStream: TMemoryStream;
MyPic: TBitMap;
begin
 
 
ExtractFromExe('BMP2',Temp);
If Temp = '' then
begin
  ShowMessage('Data NOT loaded! Has bmp1 been saved?');
end
else
begin
  Temp := DoXOR(Temp);//descramble it
  MyPic := TBitMap.Create;
  MyStream := TMemoryStream.Create;
  try
  String2Stream(temp,MyStream);
  MyPic.LoadFromStream(MyStream);
  Form1.Image1.Picture.Assign(MyPic);
  finally
  MyStream.Free;
  MyPic.Free;
  end;
end;
end;
 
//===================================================================
 
procedure TForm1.BtnSaveNewEXEClick(Sender: TObject);
begin
  Exe2File('YourNewEXE.exe');
  Exe := ''; //set Exe to empty string (save space if data no longer needed)
end;
 
//====================================================================

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ST3VOAuthor Commented:
Wow....Modified it a bit ... and it works!!!! Thanks a million :o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.