Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to add images in a panel. Each image size is 5 to 6 MB. If I add more than 35 images then it shows error that "Out of Memory". This exceptions occur when Image.FromFile(string) is executed. If less than 35 images is loaded then no error shows.

C#
if (DialogResult.OK == ofdLoadFromHDD.ShowDialog())
                {

                    string[] strArray = ofdLoadFromHDD.FileNames;
                    int count = strArray.Count();
                    DynamicGroupBox objDGB = new DynamicGroupBox(scrolledValue);
                    GroupBox gB = new GroupBox();

                    try
                    {
                        for (int i = 0; i < count; i++)
                        {
                            Bitmap bmp = (Bitmap)Image.FromFile(strArray[i]);

                            CommonInformation.SCANNING_NUMBER++;
                            objCommonUtility.replaceImageIntoDB(bmp, CommonInformation.SCANNING_NUMBER.ToString());

                            gB = objDGB.createGroupBox(bmp, CommonInformation.SCANNING_NUMBER, false);

                            pnlImageContainer.Controls.Add(gB);

                        }
                        gB.Select();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        CommonInformation.SCANNING_NUMBER--;
                    }
                }


Development Environment:
Windows XP Professional Edition
VS 2010, C#, .NET framework 4.0

How can I solve this problem?
Posted
Updated 12-Dec-11 19:28pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Dec-11 1:53am    
What do you expect for a solution: showing of 100s of such file at the same time or what?
--SA
akul123 13-Dec-11 4:01am    
Yes, I need to load images greater than 500.
alizadeh91 13-Dec-11 9:56am    
May be your RAM memory is low? How much is that?
akul123 14-Dec-11 4:57am    
Memory is 2 GB

1 solution

This is not a problem per se. You just need to remember: there is no such thing as miracle. No matter what you do, eventually you can deplete your memory if you allocate too much at the same time. Well, you can have more memory with 64-bit system, but this is not a solution as the memory is always limited.

Instead of raising this pointless concern, think about this: who needs to 35 6M images at the same time? Load them on request; loading of a file of this size is very quick. After all, present many images in the form of small thumbnail, possibly obtaining those thumbnail on the fly by re-sampling each image to a small size, which is also a fast operation. On demand, show full-size image, one two at a time. Who needs more? If you make it convenient, you won't hear from your users anything except thank you.

—SA
 
Share this answer
 
Comments
akul123 13-Dec-11 4:10am    
Let's have a look at the actual scenario. The application may need to save a book whose pages are images. At first user need to browse all the images and load them on a panel then he might change any one of the images. At last he will save all the images. That is, my application has to load 2.5 GB images at a time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900