C#应用程序在某些PC中无法正常工作 [英] c# application doesn't work properly in some pc

查看:90
本文介绍了C#应用程序在某些PC中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,该应用程序使用c#.net Framework 4中的ImageList在列表视图中加载了许多图像.图像也被压缩.当加载和压缩许多图像时,将花费很长时间.因此,我在backgroundworker中调用该方法.在后台工作人员中,我必须将图像添加到ImageList并将ImageList添加到ListView.因此,我使用了safeinvoke()方法listView1.SafeInvoke(d => d.Items.Add(item)).
一切正常.图像在列表视图中一一显示.
但是该应用程序的发布不能在某些PC上正常运行,而在其他PC上正常运行.无法正常运行意味着,如果使用OpenFileDialog加载了100张图像,然后加载了一些图像并将其添加到列表视图,然后自动停止了加载,而没有将所有图像添加到列表视图中,并且没有异常显示.

我已经花了很多时间来解决这个问题,但无法找出问题所在. .问题出在哪儿?有谁能够帮助我?


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?


 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

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

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



并添加了try-catch,如下所示:



and added try-catch as follows:

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);

                                    }



在将某些图像加载为"OutOfMemoryException"后,它将显示错误消息.

以下行很可能会创建异常:

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

但是图像文件没有损坏,并且文件扩展名为.JPG.

如何摆脱这个问题?



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?

推荐答案

开始

这篇关于C#应用程序在某些PC中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆