Change the image for Timage at runtime lazarus / 39-Graphics ---------------------------------------------------------------------- torbente · 2014-08-26 09:30 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 ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-26 09:43 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 ---------------------------------------------------------------------- howardpc · 2014-08-26 10:09 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 · 2014-08-26 10:30 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. ---------------------------------------------------------------------- minesadorada · 2014-08-26 10:45 ImageList1.GetBitmap(0,Image1.Picture.Bitmap); does transfer from the imagelist1 to image1.  Strange method naming indeed. ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-26 10:54 Quote from: minesadorada on August 26, 2014, 12:45:55 pmImageList1.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. ---------------------------------------------------------------------- torbente · 2014-08-26 11:09 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? ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-26 11:31 Quote from: torbente on August 26, 2014, 01:09:43 pmnone 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... Quote from: torbente on August 26, 2014, 01:09:43 pmI 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 Quote from: torbente on August 26, 2014, 01:09:43 pmMaybe 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 :-) ---------------------------------------------------------------------- minesadorada · 2014-08-26 11:35 Quote from: torbente on August 26, 2014, 01:09:43 pmnone 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) ---------------------------------------------------------------------- torbente · 2014-08-26 11:54 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 ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-26 12:01 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. ---------------------------------------------------------------------- torbente · 2014-08-27 15:40 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 ---------------------------------------------------------------------- Mike.Cornflake · 2014-08-27 19:18 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