Skip to main content
 首页 » 编程设计

c#之循环中的 OutOfMemoryException 错误

2024年02月13日16lidabo

我正在尝试创建一个将文件上传到 FTP 的 Windows 应用程序。本质上,它在给定文件夹中查找 .jpeg 文件,在将 .jpg 文件上传到 FTP 服务器之前读取条形码,并将 URL 输入数据库以供我们记录。

由于文件夹中在任何给定时间都会有多个文件,我实际上是在尝试循环读取它们,并相应地处理它们。但是,每当循环再次开始时,我都会收到 OutOfMemoryException。我想弄清楚我在这里做错了什么。我在下面附加了我的代码:

private void btnProcess_Click(object sender, RoutedEventArgs e) 
{ 
    podPath = Directory.GetFiles(DestPath, "*.jpg"); 
    List<string> scans = new List<string>(podPath.Length); 
    List<string> badscans = new List<string>(); 
    byte[] imageBytes; 
    string filename, result; 
    POD conpod = new POD(); 
    OTPOD otpod = new OTPOD(); 
    ConsignmentObj scanJob; 
    //Pickup OTScan; 
    //Consolidate ccv; 
 
    for (int i = 0; i < podPath.Count(); i++ ) 
    { 
        filename = podPath[i].ToString(); 
 
        using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename)) 
        { 
            var results = barcodeReader.Decode(bm); 
            result = results.ToString(); 
            bm.Dispose(); 
        } 
 
        if (result != null) 
        { 
            //if barcode can be read, we throw the value into the database to pull out relevant information 
            if (result.Contains(ConNotePrefix)) 
            { 
                #region Consignments 
                scanJob = getCon(result.ToString()); 
                final = ImageFolder + "\\" + result.ToString() + ".jpg"; 
 
                using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename)) 
                { 
                    MemoryStream ms = new MemoryStream(); 
                    try 
                    { 
                        img.Save(ms, ImageFormat.Jpeg); 
                        imageBytes = ms.ToArray(); 
                        img.Dispose(); 
                    } 
                    finally 
                    { 
                        ms.Flush(); 
                        ms.Close(); 
                        ms.Dispose(); 
                    }  
                } 
 
                lock (filename) 
                { 
                    if (System.IO.File.Exists(filename)) 
                    { 
                        File.Delete(filename); 
                    } 
                } 
 
                using (var stream = File.Create(final)) { } 
                File.WriteAllBytes(final, imageBytes); 
                File.Delete(filename); 
 
                conpod.ConsignmentID = scanJob.ConsignmentID; 
                conpod.UserID = 1; 
                conpod.Location = ftpUrl + "//" + result.ToString() + ".jpg"; 
                conpod.rowguid = Guid.NewGuid(); 
 
                UploadFilesToFtp(ftpUrl, ftpUser, ftpPass, final, result.ToString() + ".jpg"); 
                insertPOD(conpod); 
                scans.Add(result.ToString()); 
                #endregion 
                } 
            } 
            else 
            { 
                badscans.Add(filename); 
            } 
        } 
    this.lbScans.ItemsSource = scans; 
    this.lbBadScans.ItemsSource = badscans; 
} 

FTP 方法 UploadFilesToFtp(x, x, x, x, x, x) 在这里不是问题。所有反馈将不胜感激。

请您参考如下方法:

OutOfMemoryException 也可以由方法 FromFile 抛出

时 Image 类的

The file does not have a valid image format.

or

GDI+ does not support the pixel format of the file.

所以我认为您正在阅读的其中一个图像文件有问题。一种解决方案是捕获 OutOfMemoryException 并将文件添加到 badscans。

try{ 
  using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename)) { 
    var results = barcodeReader.Decode(bm); 
    result = results.ToString(); 
    bm.Dispose(); 
  } 
} 
catch(OutOfMemoryException) { 
  badscans.add(filename); 
}