Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have developed an application that loaded many images in a listview using ImageList in c# .net framework 4. The images are also compressed. When many many images are loaded and compressed then it takes a long time. So I call the method in backgroundworker. In the backgroundworker I had to add images to ImageList and add ImageList to ListView. So I have used safeinvoke() method listView1.SafeInvoke(d=>d.Items.Add(item)).
Everything works fine. Images are displayed one by one in the listview.
But the release of the application doesn’t work properly in some pc and properly works in some other pc. Doesn’t work properly means, If 100 images are browsed using OpenFileDialog to load then some images are loaded and added to listview and then the loading is automatically stopped without adding all images to the listview and no exception shows.

I have spent many times to solve this problem but couldn’t figure out the problem. . Where is the problem? Can anybody help me?


C#
 private void bgwLoading_DoWork(object sender, DoWorkEventArgs e)
        {

            ArrayList a = (ArrayList)e.Argument;

            string[] fileNames = (string[])a[0];
           
            this.loadMultiImages(fileNames);

        }

private void loadMultiImages(string[] fileNames)        
{
            int i = 1;
            int totalFiles = fileNames.Count();

                foreach (string flName in fileNames)
                {
                    if (!flName.Contains("Thumbs.db"))
                    {
                            Bitmap newBtmap = (Bitmap)Image.FromFile(flName);

                            FileInfo fi = new FileInfo(flName);
                            long l = fi.Length;

                            if (l > compressSize)
                            {
                                    newBtmap = resizeImage(newBtmap, 1024,768) ;
                                    newBtmap = saveJpeg(IMAGE_PATH + (SCANNING_NUMBER +  
                                    ) + ".jpg", newBtmap, IMAGE_QUALITY);
                            }
                            else
                            {
                                File.Copy(flName, TEMP_IMAGE_PATH + (SCANNING_NUMBER + 1) + ".jpg");

                            }



                            if (!bgwLoading.CancellationPending)
                            {
                                CommonInformation.SCANNING_NUMBER++;
                                this.SafeInvoke(d => d.addItemToLvImageContainer(newBtmap));
                                bgwLoading.ReportProgress((int)Math.Round((double)i / (double)(totalFiles) * 100));
                             
                                i++;
                            }                        
                   }
                }
            }
            
        }

        public void addItemToLvImageContainer(Bitmap newBtmap)
        {
            imageList.Images.Add(newBtmap);
            ListViewItem item;
            item = new ListViewItem();
            item.ImageIndex = SCANNING_NUMBER - 1;
            item.Text = SCANNING_NUMBER.ToString();
            lvImageContainer.Items.Add(item);
            lvImageContainer.Items[item.ImageIndex].Focused = true;

        }


To find out the error I have modified the code as follows:

I have commented the two lines

C#
//newBtmap = resizeImage(newBtmap, 1024, 768);

// newBtmap = saveJpeg(IMAGE_PATH + scanning_number + ".jpg", newBtmap, Image_Quality );


and added try-catch as follows:
C#
try
                       {
                           Bitmap newBtmap = (Bitmap)Image.FromFile(flName);

                           File.Copy(flName, CommonInformation.TEMP_IMAGE_PATH + (CommonInformation.SCANNING_NUMBER + 1) + ".jpg");


                                   if (!bgwLoading.CancellationPending)
                                   {
                                       this.SafeInvoke(d => d.imageList.Images.Add(newBtmap));

                                       file.WriteLine("d => d.imageList.Images.Add(newBtmap)" + "\n\n");

                                       ListViewItem item;
                                       item = new ListViewItem();

                                       CommonInformation.SCANNING_NUMBER++;

                                       item.ImageIndex = CommonInformation.SCANNING_NUMBER - 1;
                                       item.Text = CommonInformation.SCANNING_NUMBER.ToString();
                                       this.SafeInvoke(d => d.lvImageContainer.Items.Add(item));

                                       bgwLoading.ReportProgress((int)Math.Round((double)i / (double)(totalFiles) * 100));

                                        this.safeInvoke(d=>d.addItemImageContainer(newBtmap))

                                  catch (Exception ex)
                                  {
                                        MessageBox.Show( ex.Message);

                                    }


It shows the error message after loading some images as "OutOfMemoryException"

Most probably the following line creates the exception:

Bitmap newBtmap = (Bitmap)Image.FromFile(flName);

But the image files are not corrupted and their file extension is .JPG.

How to get rid of this problem?
Posted
Updated 27-Feb-12 7:15am
v2
Comments
Bernhard Hiller 27-Feb-12 4:05am    
Add a try-catch to bgwLoading_DoWork. Add some logging to that function also (have you checked that (ArrayList)e.Argument is really the complete list)?

1 solution

Start here[^].
 
Share this answer
 

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