0

I'm learning my way around windows forms by making a little app to help clean up a folder on my PC with thousands of images. I'm currently storing the images as a List<FileInfo>. It works fine using

PicBox.Load(ImageFileList[count++].imgFile.FullName);

but that's a bit slow when I only need a second to look at each image.

So the problems are:

  1. How to load the images quickly
  2. How to do so in a folder where one image might be a 25kb png and the next might be a 1GB TIFF

If I open an image from the folder in Windows Photo Viewer and hold the right arrow key, it zips along with blurry images I assume are resized thumbnails and a loading message over them. In Win 10 Photos, it loads non-blurry images faster but hiccups when it hits larger files. Either of those behaviors would be fine for me.

Salvatore Ambulando
  • 402
  • 1
  • 6
  • 13
  • 1
    Avoid `Image.FromFile()`, use `Image.FromStream([path], true, false)` or `Image.FromStream([path], false, false)` (which is faster, but may cause color management issues) -- Caching helps. You can just read the first part of the header of an image file using a background task (so you discard images that you cannot show and trigger the disk cache) -- You should draw the image instead of loading it using a Control, which has its own internal validations. If you decide not to, then it's just `PicBox.Image?.Dispose(); PicBox.Image = Image.FromStream(...);` to dispose of the previous image. – Jimi Feb 03 '22 at 01:01
  • Note that, in any case, it's the GDI+ API that loads and transforms the image. Maybe take a look at [WIC](https://learn.microsoft.com/en-us/windows/win32/wic/-wic-lh). – Jimi Feb 03 '22 at 01:11
  • @Jimi thanks, I'll look into those but just to clarify - I understand caching generally but in this case are you referring to manually caching the next images in my code or to windows caching features? If it's the latter, I don't know which operations to use/avoid to get the most out of it. – Salvatore Ambulando Feb 03 '22 at 13:58
  • @Jimi Also, I tried to dispose of the image in the picbox the way you described and it didn't work which is when I found the linked question, which also didn't work. I'll give a shot to drawing the images manually in case it's some picbox thing. – Salvatore Ambulando Feb 03 '22 at 14:02
  • Drawing the Image to a device context is faster than using the internal methods / properties of a Control to show it. – Jimi Feb 03 '22 at 14:43
  • 1
    You can make use of the System cache by reading part of a file, the read-ahead feature will buffer more than that, so the next time you read that file, it will be much faster. You can also cache the next N images in full in your app, using a background task; this can of course increase the memory usage if you need to load very large files, so not recommended in all cases. -- `[PictureBox].Image?.Dispose() [...] [PictureBox].Image = [New Image]` disposes of the previous image, no doubts here. If you don't see any benefit, it's caused by something else in your code. – Jimi Feb 03 '22 at 14:54