I have now re-done how the grid add the images and buttons. Adding components to the cells used way too much memory, and it crashed if it used too much.
I have removed all components like TImage and TPicture.
It only draw visually from a TImageList instead in each cell.
Now, the grid is only using a little tbitmap. It only draws all the rest and the tbitmap on the canvas. It only checks where the user is clicking, and execute the stuff instead of having real buttons.
The grid is now working very good, and using normal memory.
It also keeps only the 50 images from the top row, and frees the images before this.
It does the some with images below the visible results.
So, only 50 small images from the beginning and only 50 images after the visible list. With thousand of lines it will not have more images at any time.
The image is a tiny bitmap that is resized from the bigger image, and is in one array.
Clicking the images loads the original image from the database, and shows it in a bigger widow.
This way, i can keep the memory use very low, and just use one tiny bitmap, and one small array for the row and col.
But it has been allot of thinking and trying. Not done easily.
I also created two extra functions that i just call when drawing items to the cells using only X and Y position in rect and size i want it to be, and it automatically draws it and resize it.
It's working very well, and fast too.