Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环中的OutOfMemoryException错误_C#_.net_Wpf_List_Image Processing - Fatal编程技术网

C# 循环中的OutOfMemoryException错误

C# 循环中的OutOfMemoryException错误,c#,.net,wpf,list,image-processing,C#,.net,Wpf,List,Image Processing,我正在尝试创建一个Windows应用程序,将文件上载到FTP。基本上,它在给定的文件夹中查找.jpeg文件,在将其上载到FTP服务器之前,它会读取.jpg文件中的条形码,并将URL输入数据库以获取记录 由于文件夹中在任何给定时间都会有多个文件,因此我基本上是在循环中读取它们,并相应地处理它们。但是,每当循环再次启动时,我会得到一个OutOfMemoryException。我在想我到底做错了什么。我在下面附加了我的代码: private void btnProcess_Click(object s

我正在尝试创建一个Windows应用程序,将文件上载到FTP。基本上,它在给定的文件夹中查找.jpeg文件,在将其上载到FTP服务器之前,它会读取.jpg文件中的条形码,并将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;
}
private void btnProcess\u单击(对象发送方,路由目标)
{
podPath=Directory.GetFiles(DestPath,*.jpg”);
列表扫描=新列表(podPath.Length);
List badscans=新列表();
字节[]图像字节;
字符串文件名,结果;
POD conpod=新POD();
OTPOD OTPOD=新OTPOD();
委托OBJ扫描作业;
//拾取OTScan;
//合并ccv;
对于(int i=0;i

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

该文件没有有效的图像格式

GDI+不支持文件的像素格式

所以我认为你正在阅读的一个图像文件有问题。一种解决方案是捕获
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);
}

当发生异常时,
Image
类的方法也可以抛出
OutOfMemoryException

该文件没有有效的图像格式

GDI+不支持文件的像素格式

所以我认为你正在阅读的一个图像文件有问题。一种解决方案是捕获
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);
}

调用
podPath.Count()
OutOfMemory
正是它听起来的样子。。您的进程已达到其内存限制的上限。我可能可以尝试podPath.Length()并查看得到的结果。谢谢。字段
barcodeReader
那是什么类型的类?也许那个类的解码方法是一个没有帮助的问题。我的意思是。。很明显,你在那里创建了一个庞大的项目列表。。这取决于从
podPath
集合开始时有多少项。有多少项?顺便问一下:
使用(var stream=File.Create(final)){}
?调用
podPath.Count()
会产生多少项
OutOfMemory
正是它听起来的样子。。您的进程已达到其内存限制的上限。我可能可以尝试podPath.Length()并查看得到的结果。谢谢。字段
barcodeReader
那是什么类型的类?也许那个类的解码方法是一个没有帮助的问题。我的意思是。。很明显,你在那里创建了一个庞大的项目列表。。这取决于从
podPath
集合开始时有多少项。有多少?这是故意的吗?顺便说一句:
使用(var stream=File.Create(final)){}
?对于这些格式问题来说似乎是一种奇怪的异常类型。这个
OutOfMemoryException
是否包装了一个内部异常(或有一条消息)来解释真正的问题?@groverboy我认为它包含一些GDI+错误代码或其他信息,但我无法验证它,因为我没有坏的图像文件。对于这些格式问题来说,这似乎是一种奇怪的异常类型。这是否
OutOfMemoryExcept