[SOLVED] How can I initialize a Picture insde a TImage? lazarus / 39-Graphics ---------------------------------------------------------------------- ratmalwer · 2022-02-04 00:07 I want to build an array with different pictures inside (its not allways a jpg). So I use the TImage without a Form. For doucumentation of the problem I built 3 procedures. 1. loading picture with a Form (works as expected) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnSampleWithFormClick(Sender: TObject);var i : integer;begin   //image1 is on a Form     for i:= 0 to listbox1.Items.Count - 1 do begin         image1.Picture.graphic.LoadFromFile(listbox1.Items[i]); //The file wanted to load     end;end;   2. Loading picture with a workaround (this cannot be the solution) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnPreloadDummyClick(Sender: TObject);var i : integer;    image2 : Timage;begin     //image2 is only in Memory     for i:= 0 to listbox1.Items.Count - 1 do begin         image2 := timage.create(self);         image2.Picture.LoadFromFile('P:\Dummy- image.jpg');         image2.Picture.graphic.LoadFromFile(listbox1.Items[i]);  //The file wanted to load          //Image 1 can be displayed without problems on the Form         image1.Picture.assign(image2.Picture);     end;end; 3. Loading picture the way it shoud be (But ths gives an Accesserror) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnSampleWhitoutFormClick(Sender: TObject);var i : integer;    image2 : Timage;begin     //image2 is only in Memory     for i:= 0 to listbox1.Items.Count - 1 do begin         image2 := timage.create(self);         //image2.Picture.Create;          does not work         //image2.Picture.Graphic.Create;  does not work       image2.Picture.graphic.LoadFromFile(listbox1.Items[i]);   //The file wanted to load     end;end;   Can someone point me the right way? ---------------------------------------------------------------------- Josh · 2022-02-04 00:25 hi in code below image1 is a timage on the form; the below code loads the file and then places it in the form timage. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var i:timage;begin  i:=timage.Create(nil);  i.Picture.LoadFromFile('a valid file.jpg');  image1.Picture.Bitmap:=i.Picture.Bitmap;  i.Free;end;  i suspect your issue is that you keep creating the image in the loop; either free it in the loop; or create it once before the loop and free it after. ---------------------------------------------------------------------- ratmalwer · 2022-02-04 00:33 Thanks Josh this is basicly the same solution as I have (2nd). Is there no way dooing this without loading a file I do not want to load? Notice that the Error rises when I want to load the graphic: (image2.Picture.graphic.LoadFromFile) witch is for PNG or GIF s. Markus ---------------------------------------------------------------------- Josh · 2022-02-04 01:09 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnSampleWhitoutFormClick(Sender: TObject);var i : integer;    image2 : Timage;begin     //image2 is only in Memory     image2 := timage.create(self);  //only create it once     for i:= 0 to listbox1.Items.Count - 1 do     begin       image2.Picture.LoadFromFile(listbox1.Items[i]);   //The file wanted to load     end;     image2.Free; // finish so free itend; ---------------------------------------------------------------------- ratmalwer · 2022-02-04 01:17 but I need:  image2.Picture.graphic.LoadFromFile(listbox1.Items); ---------------------------------------------------------------------- bytebites · 2022-02-04 19:39 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  pic: TPicture;begin  pic:=TPicture.create;  pic.LoadFromFile('test.png');  Image1.Picture.Assign(pic);   pic.free;... ---------------------------------------------------------------------- ratmalwer · 2022-02-04 23:56 Thanks bYtebites Tpicture was the qlue. I was following an example with .. graphics and that misslead me. ... and now I change everything back again  Cheers Markus