In my program i am creating some big pictures (Image objects), and saving them to disk. Then I add them to a list List<Image> but after saving 50 pictures and adding them as Image objects to my imageList it eats up a loooooot of memory. I tried doing this on 50 images and just saving pure image object, my program went up to 160 MB in process manager. So I must find out a way of saving the pictures and adding them to the list without the program eating up all memory.
So I have a couple of solutions and I would love to hear what you think about them or if you have any better ones.
- Compress the
byte[]array of the image object. - Compress the stream of the memorysteam object.
- Convert the
byte[]array of the image object to string and then compress the string.
I am doing this in c#.
Do not compress your images using the .Net DeflateStream or such (as there is a known issue where it actually increases the size of the data, in all cases) – save it directly to something like a .png.
Make sure you dispose the images that you created (after you have saved them).
You can’t compress the images in memory – because Windows GDI (which .Net uses) requires the images in, essentially, uncompressed bitmap form (so when you load a compressed image it will get decompressed).
You should look at loading them on-demand. Here is a class similar to
ImageListwhich you may find useful: