Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way of saving image file and to save memory

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.

  1. Compress the byte[] array of the image object.
  2. Compress the stream of the memorysteam object.
  3. Convert the byte[] array of the image object to string and then compress the string.

I am doing this in c#.

like image 846
syncis Avatar asked Aug 18 '11 21:08

syncis


2 Answers

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.

Bitmap bmp;
// ...
bmp.Save("foo.png", System.Drawing.Imaging.ImageFormat.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 ImageList which you may find useful:

public class DelayedImagedList : Component
{
    // Item1 = Dispose for the image.
    // Item2 = At creation: the method to load the image. After loading: the method to return the image.
    // Item3 = The original filename.
    private List<Tuple<Action, Func<Image>, string>> _images = new List<Tuple<Action,Func<Image>,string>>();
    private Dictionary<string, int> _imageKeyMap = new Dictionary<string, int>();

    // Access images.
    public Image this[string key] { get { return _images[_imageKeyMap[key]].Item2(); } }
    public Image this[int index] { get { return _images[index].Item2(); } }
    public int Count { get { return _images.Count; } }

    // Use this to add an image according to its filename.
    public void AddImage(string key, string filename) { _imageKeyMap.Add(key, AddImage(filename)); }
    public int AddImage(string filename)
    {
        var index = _images.Count;
        _images.Add(Tuple.Create<Action, Func<Image>, string>(
            () => {}, // Dispose
            () => // Load image.
            {
                var result = Image.FromFile(filename);
                // Replace the method to load the image with one to simply return it.
                _images[index] = Tuple.Create<Action, Func<Image>, string>(
                    result.Dispose, // We need to dispose it now.
                    () => result, // Just return the image.
                    filename);
                return result;
            }, 
            filename));
        return index;
    }

    // This will unload an image from memory.
    public void Reset(string key) { Reset(_imageKeyMap[key]); }
    public void Reset(int index)
    {
        _images[index].Item1(); // Dispose the old value.
        var filename = _images[index].Item3;

        _images[index] = Tuple.Create<Action, Func<Image>, string>(
            () => { }, 
            () =>
            {
                var result = Image.FromFile(filename);
                _images[index] = Tuple.Create<Action, Func<Image>, string>(
                    result.Dispose, 
                    () => result, 
                    filename);
                return result;
            }, 
            filename);
    }

    // These methods are available on ImageList.
    public void Draw(Graphics g, Point pt, int index) { g.DrawImage(this[index], pt); }
    public void Draw(Graphics g, int x, int y, int index) { g.DrawImage(this[index], x, y); }
    public void Draw(Graphics g, int x, int y, int width, int height, int index) { g.DrawImage(this[index], x, y, width, height); }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            foreach (var val in _images) { val.Item1(); }
            _images.Clear();
            _imageKeyMap.Clear();
        }
        base.Dispose(disposing);
    }
}
like image 67
Jonathan Dickinson Avatar answered Oct 04 '22 18:10

Jonathan Dickinson


why compress? Surely you don't have to show all images at the same time (not in full resolution) - so either create smaller thumbs or show only a small subset.

like image 27
Random Dev Avatar answered Oct 04 '22 20:10

Random Dev