Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6360943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:42:48+00:00 2026-05-24T23:42:48+00:00

In my program i am creating some big pictures ( Image objects), and saving

  • 0

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#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:42:48+00:00Added an answer on May 24, 2026 at 11:42 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a program I'm writing I'm creating some objects with a start date (with
I creating a program which send newsletter with a background image. It works fine
I'm currently creating a C# program which will be fetching some data over https
I'm creating some sort of frontend for a program. To launch the program I'm
I'm creating a utility program which draws some (user customizable) icons onto other windows
I am creating a JavaScript tooltip program and wonder if it's alright to add
I have a multi-threaded Delphi program creating multiple instance of some classes, and I
I'm creating a program that gets data from a server using AJAX/JSON and then
I'm creating a byacc program to parse some input, and I'm having trouble resolving
Need some help for creating a File and String search engine. The program needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.