Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add 1000 images (each size is (40 to 100) KB) in a panel at run time in a desktop application. At first user browses all the images and load them on a panel. When it loads images one after another then memory usage shown in the task manager increases rapidly and after a certain number of images it shows the “Out of Memory Exception”. Where is the fault in my code?

Before loading the 700 images task manager shows 1.05 GB memory usage. After loading task manager shows 2.04 GB and 2 GB RAM overflows
int picnumber = 0;
int numberOfImages = 1000;


for (int i = 0; i < numberOfImages; i++)
{
    GroupBox gBox = new GroupBox();

    picnumber++;


    ////////////////////////////////

    // calculate the position of the groupbox where it is placed.
    if ((picnumber % 3) == 1)
    {
        x = initX;
    }
    else
    {
        if ((picnumber % 3) == 0)
        {
            x = initX + 2 * (130 + 20);
        }
        else
        {
            x = initX + 130 + 20;
        }

    }
    ///////////////////////////////////


    System.Drawing.Point CurrentPoint;

    CurrentPoint = panel1.AutoScrollPosition;
    y = initY + ((picnumber - 1) / 3) * (130 + 20) - (Math.Abs(panel1.AutoScrollPosition.Y));

    gBox.Text = picnumber.ToString();

    //place the groupbox in the appropriate position.
    gBox.Location = new System.Drawing.Point(x, y);
    gBox.Size = new System.Drawing.Size(130, 130);

    Bitmap btmap = new Bitmap(@"E:\43.jpg");
    // attach the image to the groupbox
    gBox.BackgroundImage = btmap;
    gBox.BackgroundImageLayout = ImageLayout.Stretch;
    // add the groupbox that contains image to the panel.
    panel1.Controls.Add(gBox);


But I have seen some applications that can load huge number of images and takes memory that is negligible, for example, “Batch Image Resizer”( http://www.jklnsoft.com/)

Before loading the 700 images task manager shows 1.05 GB memory usage. After loading task manager shows 1.06 GB
How does the application handle memory? What mechanism do they follow?
Development environment: C#.net framework 4, windows xp, Visual Studio 2010, RAM: 2 GB
Posted

They don't do it like that - they draw only the images that the user sees - at no time do they load 1000 images and hope it will work. The most they might do is load the image, take a small thumbnail of it, and unload the image. The thumbnails are then available for display as the user needs them. You are trying to keep 1000 bitmaps, each huge in comparison!
 
Share this answer
 
Comments
akul123 25-Jan-12 0:52am    
In many ways I tried to release the memory. When I close the form it still consumes the memory.
Could you please tell me how could i unload the image?
OriginalGriff 25-Jan-12 3:13am    
Use the Dispose method on images as you go - or enclose the definition in a using block to do it for you.
Note that when you close a form, nothing is unloaded from memory until the garbage collector gets called in to make space, and the the memory footprint of your application (as far as the outside world is concerned) will not shrink - once .NET reads to allocate more memory it does not give it back until the application exits.
In relation to OriginalGriff's answer, here is the code for resizing the images or creating thumbnails:

C#
public class Tools
{
    public static Image ResizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }
}


You might need to save the thumbnails as temporary files and retrieve it only when you need it instead of leaving it hanging on the user's memory.

Hope this helps.
 
Share this answer
 
Comments
akul123 25-Jan-12 0:57am    
Using your mentioned method I compressed the image size from Megabyte to KiloByte. Then the small size image is loaded. But the problem remains.
unknowndentified10111 25-Jan-12 1:04am    
Do not load them all at once. Save the all the image file name in a List<string>, or something, then load the images only when you are about to display them.
you can try this..

1) at a same time you need not load the all 1000 images...
2) you can load only part of images that will user can see at a time..
3) as it will scroll or change the screen unload previous images and load new images
4) so you memory issue will be solved...
 
Share this answer
 
Comments
akul123 25-Jan-12 1:00am    
Please tell me how could i unload previous images. When I close the form containing the images the memory usage in the task manager shows the previous used size. That is the memory is still occupied after unloading the form.

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