Lazy loading a very large amount of images in a listbox #13605
Replies: 4 comments 14 replies
-
you can bind to Task. This Task can look up the img in the cache and if not present add it first. |
Beta Was this translation helpful? Give feedback.
-
You may want to have a loading queue to avoid your code load all the visible images at the same time (and so access the disk at the same time) if they are many of them, and return a placeholder immediately in the meantime. Depending of the file format, the file may already embed a thumbnail, but Avalonia do not provide a way to access it like WPF do, neither as Skia as far as I know, but there is SKCodec which may decode smaller version of the bitmap, probably what DecodeToHeight do but with more control, and you can check if you still have the memory leak. You may also give a try to ItemsRepeater with UniformGridLayout which seems smoother than ListBox in my tests. |
Beta Was this translation helpful? Give feedback.
-
I think @dbriard means don't load the entire image data from the file, doesn't seem like you mentioned that decodeToHeight is not the same, sounds like that loads all teh bitmap , then resamples it. thats super expensive. should be 30 kb t megabtypes, not gigabtyes for this. SKCodec data https://learn.microsoft.com/en-us/dotnet/api/skiasharp.skcodec?view=skiasharp-2.88 also they changed this in versin 3 if it has problems.. since this is an old api i would jus ask chatgpt to code you loading and sample just a thumbnail using a this codek that to a specified size for thumbnails while you are streaming it.. allocating just 100x100 image x 32 rbga, or even make the whole panel or list control with that in it .. ( after) i thimk i tried it a while back adn it did it for me , i was blown away. but its very moody as far as coding for you.. depends on the day, your history with it , etc. I was asking if it could make a file explorer in avalonia with thumbnails too. but i don't think it did the codex stuff.. that should make 1000x difference imagine a list of cad files, each blueprint size. thats sever GB per file. we never loaded teh whole file. just the part we were going to map to the viewport. . dont use a profiler just try with a few giant images like 4k , and just loading the data is noticiable.. it looks like they can skip memory allocatoins, resample various quality ( like a mipmap) then allocate close to the target image, and make sure you close the file you are listing. generally on the thumnail topic: |
Beta Was this translation helpful? Give feedback.
-
I was curious so I asked it to code it. Result below.. also I offer to try some bot stuff for image sharp. Tldr the code is at bottom.
I think your should do it off the background thread. Its code did compile.
should work.but Didn't test. It makes a cache somewhere. It can code a
filewacher. And do the map amd the rest of you insist on it. Then ud have to port to the new
avalonia maybe since it's trained on the older one. If it flickers or leaks I'd try it. There is a cache from file manager, it's in a db file it advised me not to use that .
I never use async await. Especially spawned from the UI thread.. It's going to load the whole file synchronously to decompress it, I use run a main a worker thread anywayis and keep it spinning .. keep my UI thread lockless.. awat reentrant can be a killer.
Because of the 2d full decompress. Fractal, I removed that async uwpcancer from all my code.
I'm not a web developer. It's all smooth and no
syncs and locks and if skia3 isn't thread safe if should be. So it's below
.
because im never want to code again outside of NLP, I can do, it won't. I
have to use the web API. It's codes faster and better than me and I've 30
yrs at a pc.. and it's not my fav waste of time anymore. Now llms , nlp
are my waste of time, because they are teasing but half baked. But I
don't want to try or use a keyboard or press compile and say fix it .
Anyways it's flaky and I can't be a fanboy I check it now and then. And
tell the team what I'm working on and why, and and thumps up down .
Things improve .. regress, generally it gets better. I want to design
So I talked to it , your right codex cant decompress thunmbails line by line , except for
tiff files. Those are used in monochrome and other archival docs. And
blueprints. They can be mutipage. The windows Apiand or dotnet might
give access to embedded thumbs i didn't ask how .
I looked a skia3 and they changed a lot on this. I ask d it to do it for
tiff files , multipage,.line skipping, it said it was too much work,
thumbs down I paid it. Two licenses. I also want to help image sharpe try
this using the bot. I know it can do this and he's struggling to get
revenue for like 10 years of work. But image engines aren't worth a dime
without this memory management pipeline to viewport, you can wrap an unmanaged one . Imagesharp is simd and unmanaged and might help if ur are ok with the license.
However the code below might be good enough. Or look a t skia3. Sorry for typos my ai is broken I'm on phone.
using SkiaSharp;
using System.Threading.Tasks;
int numberOfProcessors = Environment.ProcessorCount;
SKBitmap[] buffers = new SKBitmap[numberOfProcessors];
// Pre-allocate buffers
for (int i = 0; i < numberOfProcessors; i++)
{
buffers[i] = new SKBitmap(width, height); // Specify appropriate width
and height
}
Parallel.For(0, images.Length, (i) =>
{
int bufferIndex = i % numberOfProcessors;
string imagePath = images[i]; // Assuming 'images' is an array of image
paths
// Load and resize image using SkiaSharp into the buffer
using (var inputStream = File.OpenRead(imagePath))
{
using (var original = SKBitmap.Decode(inputStream))
{
using (var resized = original.Resize(new SKImageInfo(width,
height), SKFilterQuality.High))
{
buffers[bufferIndex] = resized;
// Process the resized image as needed
}
}
}
// Here you would save the thumbnail or add it to the cache
});
// Cleanup
for (int i = 0; i < numberOfProcessors; i++)
{
buffers[i]?.Dispose();
}
In this example, the number of buffers equals the number of processors.
Each thread processes a different image and uses its designated buffer.
This example is quite basic and assumes that the resizing operation does
not exceed the buffer's capacity. In a full implementation, you'd also
handle exceptions, synchronization issues, and potentially more complex
buffer management.
…On Tuesday, January 9, 2024, thevortexcloud ***@***.***> wrote:
What I have works, I don't need to use chat GPT for that. It would
probably give me a lot of broken/invalid code anyway. There are currently
Skia issues with using the DecodeTo methods that makes them unable to
load a lot of images.
I don't have to generate new thumbnails often anyway.
—
Reply to this email directly, view it on GitHub
<#13605 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AD74XGIJGJZL6WOJIUOUNX3YNWVT5AVCNFSM6AAAAAA7KPPTFGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DANZRG4ZTS>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I want to make an image gallery sort of thing, preferably with either no paging or invisible paging (the idea being the user scrolls through a list of thumbnails) and no scroll stuttering. However I can't seem to find a good way to lazy load the thumbnails/list box items that are actually visible in the
ListBox
. Preloading all the images is not going to work due to sheer number of images being displayed may quickly lead to people running out of memory (not to mention the long processing times if it has to load a lot of non cached images that are not the correct thumbnail size).Does anybody have some ideas about possible ways to do this? I am still in the prototyping stage so I am not too fussed if I need to throw this all out and try something else.
Beta Was this translation helpful? Give feedback.
All reactions