Recent

Author Topic: Change the image for Timage at runtime  (Read 17440 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Change the image for Timage at runtime
« on: August 26, 2014, 11:30:50 am »
I need to change the image that a timage will show. I already loaded all the possible images in a image list, but i dont have idea how i can assign now different images to it.

I want do something like that...

Image1.picture := imagelist.index(1);

i try for a while and check the forums, but i got information about other uses of image list. I guess it is something very easy but i can not figure it by myself.

Thanks
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Change the image for Timage at runtime
« Reply #1 on: August 26, 2014, 11:43:19 am »

Haven't done this myself, but a google for a Delphi equivalent code returned:
Code: [Select]
ImageList1.GetBitmap(0,Image1.Picture.Bitmap);The first parameter in GetBitmap is the index into ImageList1.   I'm presuming something similar will work with LCL.


Looking at the online help, I suspect this will indeed work:
http://lazarus-ccr.sourceforge.net/docs/lcl/imglist/tcustomimagelist.getbitmap.html
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Change the image for Timage at runtime
« Reply #2 on: August 26, 2014, 12:09:31 pm »
Torbente is not trying to fill his imagelist with data, but to draw in his TImage using existing imagelist data.
You can use one of the imagelist drawing methods (Draw, StretchDraw). For example this draws the image at index 0 on Image1:

Code: [Select]
  Image1.Picture.Bitmap.SetSize(ImageList1.Width, ImageList1.Height);
  ImageList1.Draw(Image1.Picture.Bitmap.Canvas, 0, 0, 0);   

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Change the image for Timage at runtime
« Reply #3 on: August 26, 2014, 12:30:45 pm »
Happy to believe you.  The naming of GetBitmap was confusing me, but the help is written as is the Bitmap is returned to the parameter, not from the parameter.  Thanks for the correction.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Change the image for Timage at runtime
« Reply #4 on: August 26, 2014, 12:45:55 pm »
ImageList1.GetBitmap(0,Image1.Picture.Bitmap); does transfer from the imagelist1 to image1.  Strange method naming indeed.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Change the image for Timage at runtime
« Reply #5 on: August 26, 2014, 12:54:23 pm »
ImageList1.GetBitmap(0,Image1.Picture.Bitmap); does transfer from the imagelist1 to image1.  Strange method naming indeed.
I was just about to boot up Lazarus to test this out.  As I found this code in a Delphi forum, looks like we can blame Borland for the confusion :-)   
Either way, both code snippets posted here should work.  I personally prefer @howardpc's solution, as at least I can read that and work out what's happening.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Change the image for Timage at runtime
« Reply #6 on: August 26, 2014, 01:09:43 pm »
none of both methods work for me. I did not receive any error, but the image1 is still empty. I checked the imagelist and it have share images = yes. Maybe i need create the image and not drag it from the IDE?
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Change the image for Timage at runtime
« Reply #7 on: August 26, 2014, 01:31:01 pm »
none of both methods work for me. I did not receive any error, but the image1 is still empty.
Hmmm.  While I haven't tested GetBitmap, @howardpc's code should work.

I suspect something else is going on...

I checked the imagelist and it have share images = yes.

I've never heard of ShareImages before.  Help says it's an unused property, so I doubt that's the problem...
http://lazarus-ccr.sourceforge.net/docs/lcl/imglist/tcustomimagelist.shareimages.html

Maybe i need create the image and not drag it from the IDE?

I'm not sure what you mean by this.   How did you drag an image from the IDE?

EDIT: And could you post the actual code you're using.  That way we can see if anything else is amiss?   Many thanks :-)
« Last Edit: August 26, 2014, 01:35:57 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Change the image for Timage at runtime
« Reply #8 on: August 26, 2014, 01:35:53 pm »
none of both methods work for me. I did not receive any error, but the image1 is still empty. I checked the imagelist and it have share images = yes. Maybe i need create the image and not drag it from the IDE?
When I tested the code I populated the image list by right-clicking on the control to bring up a pop-up menu with 'Imagelist Editor' as the first entry.
I added a couple of images using that and put an Image control on the form, then assigned ImageList1.GetBitmap(0,Image1.Picture.Bitmap); to a button click event.  It worked for me, without changing any default properties.
(Windows Vista + current release versions Laz and FPC)
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Change the image for Timage at runtime
« Reply #9 on: August 26, 2014, 01:54:49 pm »
Sorry, was my mistake, i forgot to start the timer.

now my code is:

Code: [Select]
procedure TForm2.Timer1Timer(Sender: TObject);
var
a,b,c:integer;
begin
  if spining = true then
     begin
     randomize();
     a := random(7);
     b:= random(7);
     c := random(7);
     Image1.Picture.Bitmap.SetSize(ImageList1.Width, ImageList1.Height);
     ImageList1.Draw(Image1.Picture.Bitmap.Canvas, 0, 0, a);
     Image2.Picture.Bitmap.SetSize(ImageList1.Width, ImageList1.Height);
     ImageList1.Draw(Image2.Picture.Bitmap.Canvas, 0, 0, b);
     Image3.Picture.Bitmap.SetSize(ImageList1.Width, ImageList1.Height);
     ImageList1.Draw(Image3.Picture.Bitmap.Canvas, 0, 0, c);
     end;
end;

Is a simple slot machine, but the images changes randomly only the first time. timer1 is enabled and have an interval of 250. Maybe i must erase the images??? The imagelist have 8 diferent images.

EDIT

I fixed it adding imageX.picture.clear;

Thanks a lot

« Last Edit: August 26, 2014, 02:05:09 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Change the image for Timage at runtime
« Reply #10 on: August 26, 2014, 02:01:08 pm »
Move Randomize() out to FormCreate or somewhere, it only needs to be called once.   I have a vague memory of a similar problem being solved by ensuring randomize was only called once.

(As a minor, unrelated, optimisation, you only need to call all those SetSizes once, so they can be moved to FormCreate as well)

No need to erase the images, the rest of the code appears fine.

If it's not Randomize, I'd say it's down to the variable spinning.  Where are you setting it to false? 

EDIT: I see your edit :-)   Good to see it's resolved.
« Last Edit: August 26, 2014, 02:16:11 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Change the image for Timage at runtime
« Reply #11 on: August 27, 2014, 05:40:53 pm »
Now. to show the line that is winning, i need paste many diffrent images from another imagelist in another Timage, keeping their alpha chanel. Actually, i dont have idea at all how it could be done.

thanks
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Change the image for Timage at runtime
« Reply #12 on: August 27, 2014, 09:18:46 pm »
Might be worth creating a new thread for how to use Alpha channels.  I've no idea, but at least if the thread title is more appropriate, you've got a better chance of the right person seeing the question :)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

 

TinyPortal © 2005-2018