Hello

Hello, 
I would like to ask for  help of the experts. 
In this method I use ADVStringGrid from TMS Components to display web images in the cells. 
Images are loaded via Indy component IdHTTP1 into a memory stream then I add them to array of JPEG images.
In the next step, I load them into Grid from Array of Pictures calling a method AddPictures from AdvStringGrid.

The problem I encouter is how to free the picture objects created every time a function is called without crashing the grid 
since it is still referring to them.

arrayPictures[j] := TPicture.Create;



procedure TForm1.gridStationMasterClickCell(Sender: TObject; ARow,
  ACol: Integer);
var
  sqlQuery: string;
  oraStations : TOraQuery; 
  qryResults : TStringList;
  rows, cols, i, j : integer;
   imageUrlPath : string;
  arrayMS : array of TMemoryStream;
  arrayJpg : array of TJPEGImage;
  arrayPictures : array of TPicture;
  pictCounter : integer;

begin
  // Click on Photo Column
    if(ACol = 0) then
    begin
      sqlQuery := thumbNailsImagesSql + gridStationMaster.Cells[1,ARow] + '''';
      pictCounter := -1;
         try

            oraStations := getQueryResults(sqlQuery);
            rows := oraStations.RecordCount; // number of Records - Rows
            cols := oraStations.FieldCount;

            SetLength(arrayMS,cols);
            SetLength(arrayJpg,cols);
            SetLength(arrayPictures,cols);

            gridImageViewer.ClearAll;

            gridImageViewer.RowCount := cols;
            gridImageViewer.ColCount := rows;
 
             for i := 0 to rows-1 do
              begin
                for j := 0 to cols -1  do
                  begin
                    imageUrlPath :=  imageURL +oraStations.Fields.Fields[j].AsString;
                    inc(pictCounter);
                    try
                      arrayMS[j] := TMemoryStream.Create;
                      arrayJpg[j] := TJPEGImage.Create;
                      arrayPictures[j] := TPicture.Create;

                      IdHTTP1.Get(imageUrlPath, arrayMS[j]);
                      if (arrayMS[j].Size > 0) then
                      begin
                      
                        //Store images in the array of MemoryStream objects
                        arrayMS[j].Seek(0,soFromBeginning);
                        //Store objects from Stream into array of JPEGS
                        arrayJpg[j].LoadFromStream(arrayMS[j]);
                        //Store JPEGs into the array of pictures in order to display them
                        arrayPictures[j].Assign(arrayJpg[j]);

                        gridImageViewer.AddPicture(pictCounter,0,arrayPictures[j],false,Shrink,0,haCenter,vaCenter);
                        
                      end; // end IF (arrayMS[j].Size > 0) 

                    arrayMS[j].Free;
                    arrayJpg[j].Free;

                    except
                      on E: EIdHTTPProtocolException do
                    begin
                      if E.ErrorCode = 404 then
                      begin
                        
                        arrayMS[j].Free;
                        arrayJpg[j].Free;
                        //arrayPictures[j].Free;

                        dec(pictCounter);
                        Continue;
                      end;
                    end; // end do
                  end; // end TRY
                end; //end j loop - cols

                oraStations.Next;
              end; // end i loop - rows
         finally
            oraStations.Session.Free;
            oraStations.Free;
         end;
    end; // end ACol = 0
    
  end;

Comments