Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 为什么我';当试图将硬盘上的所有图像放入一个列表时,是否出现异常?_C#_.net_Winforms - Fatal编程技术网

C# 为什么我';当试图将硬盘上的所有图像放入一个列表时,是否出现异常?

C# 为什么我';当试图将硬盘上的所有图像放入一个列表时,是否出现异常?,c#,.net,winforms,C#,.net,Winforms,在form1构造函数中: imageslist = Directory.EnumerateFiles(dir, "*.bmp", SearchOption.AllDirectories) .AsParallel() .Select(path => new Bitmap(path)) .Select(bmp => ResizeImage(bmp, 100, 100)) .ToList(); 例外情况是: System.Core.dll中发生类型为“Sy

在form1构造函数中:

imageslist = Directory.EnumerateFiles(dir, "*.bmp", SearchOption.AllDirectories)
    .AsParallel()
    .Select(path => new Bitmap(path))
    .Select(bmp => ResizeImage(bmp, 100, 100))
    .ToList();
例外情况是:

System.Core.dll中发生类型为“System.AggregateException”的未处理异常

其他信息:发生了一个或多个错误

System.AggregateException was unhandled
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=System.Core
  StackTrace:
       at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)
       at System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState groupState, PartitionedStream`2 partitions, SynchronousChannel`1[] channels, TaskScheduler taskScheduler)
       at System.Linq.Parallel.DefaultMergeHelper`2.System.Linq.Parallel.IMergeHelper<TInputOutput>.Execute()
       at System.Linq.Parallel.MergeExecutor`1.Execute()
       at System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId)
       at System.Linq.Parallel.PartitionedStreamMerger`1.Receive[TKey](PartitionedStream`2 partitionedStream)
       at System.Linq.Parallel.SelectQueryOperator`2.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings)
       at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
       at System.Linq.Parallel.SelectQueryOperator`2.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings)
       at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
       at System.Linq.Parallel.ScanQueryOperator`1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
       at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
       at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
       at System.Linq.Parallel.QueryOperator`1.GetOpenedEnumerator(Nullable`1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings)
       at System.Linq.Parallel.QueryOpeningEnumerator`1.OpenQuery()
       at System.Linq.Parallel.QueryOpeningEnumerator`1.MoveNext()
       at System.Linq.ParallelEnumerable.ToList[TSource](ParallelQuery`1 source)
       at LoadingImages.Form1..ctor() in D:\C-Sharp\LoadingImages\LoadingImages\LoadingImages\Form1.cs:line 29
       at LoadingImages.Program.Main() in D:\C-Sharp\LoadingImages\LoadingImages\LoadingImages\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2147024809
       Message=Parameter is not valid.
       Source=System.Drawing
       StackTrace:
            at System.Drawing.Bitmap..ctor(String filename)
            at LoadingImages.Form1.<>c.<.ctor>b__6_0(String path) in D:\C-Sharp\LoadingImages\LoadingImages\LoadingImages\Form1.cs:line 31
            at System.Linq.Parallel.SelectQueryOperator`2.SelectQueryOperatorEnumerator`1.MoveNext(TOutput& currentElement, TKey& currentKey)
            at System.Linq.Parallel.SelectQueryOperator`2.SelectQueryOperatorEnumerator`1.MoveNext(TOutput& currentElement, TKey& currentKey)
            at System.Linq.Parallel.StopAndGoSpoolingTask`2.SpoolingWork()
            at System.Linq.Parallel.SpoolingTaskBase.Work()
            at System.Linq.Parallel.QueryTask.BaseWork(Object unused)
            at System.Linq.Parallel.QueryTask.<>c.<.cctor>b__10_0(Object o)
            at System.Threading.Tasks.Task.InnerInvoke()
            at System.Threading.Tasks.Task.Execute()
       InnerException:
System.AggregateException未处理
HResult=-2146233088
消息=发生了一个或多个错误。
Source=System.Core
堆栈跟踪:
位于System.Linq.Parallel.QueryTaskGroupState.QueryEnd(布尔userInitiatedDispose)
在System.Linq.Parallel.SpoolingTask.SpoolStopAndGo[TInputOutput,TIgnoreKey](QueryTaskGroupState,groupState,PartitionedStream`2个分区,SynchronousChannel`1[]个通道,TaskScheduler TaskScheduler)
位于System.Linq.Parallel.DefaultMergeHelper`2.System.Linq.Parallel.IMergeHelper.Execute()
位于System.Linq.Parallel.MergeExecutor`1.Execute()
在System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2个分区,Boolean ignoreOutput,ParallelMergeOptions选项,TaskScheduler TaskScheduler,Boolean isOrdered,CancellationState CancellationState,Int32 queryId)
在System.Linq.Parallel.partitionedstreammerge`1.Receive[TKey](PartitionedStream`2 PartitionedStream)
在System.Linq.Parallel.SelectQueryOperator`2.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream,iPPartitionedStreamRecipient`1 recipient,布尔首选条带化,QuerySettings设置)
在System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
在System.Linq.Parallel.SelectQueryOperator`2.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream,iPPartitionedStreamRecipient`1 recipient,布尔首选条带化,QuerySettings设置)
在System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
位于System.Linq.Parallel.ScanQueryOperator`1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1收件人)
在System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1个收件人)中
在System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1个收件人)中
位于System.Linq.Parallel.QueryOperator`1.GetOpenedNumerator(可为null的`1合并选项、布尔抑制顺序、布尔前效果、查询设置查询设置)
在System.Linq.Parallel.QueryOpenEnumerator`1.OpenQuery()中
在System.Linq.Parallel.QueryOpeningEnumerator`1.MoveNext()中
在System.Linq.ParallelEnumerable.ToList[TSource](ParallelQuery`1 source)
在D:\C-Sharp\LoadingImages\LoadingImages\LoadingImages\LoadingImages\Form1.cs:第29行中的LoadingImages.Form1..ctor()处
在D:\C-Sharp\LoadingImages\LoadingImages\LoadingImages\LoadingImages\Program.cs中的LoadingImages.Program.Main()处:第19行
位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext、ContextCallback回调、对象状态、布尔值preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔保存SyncCTX)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
内部异常:
HResult=-2147024809
Message=参数无效。
Source=系统图
堆栈跟踪:
位于System.Drawing.Bitmap..ctor(字符串文件名)
在D:\c-Sharp\LoadingImages\LoadingImages\LoadingImages\LoadingImages\Form1.cs中的LoadingImages.Form1.c.b\u 6\u 0(字符串路径)处:第31行
在System.Linq.Parallel.SelectQueryOperator`2.SelectQueryOperator分子`1.MoveNext(TOutput¤tElement,TKey¤tKey)
在System.Linq.Parallel.SelectQueryOperator`2.SelectQueryOperator分子`1.MoveNext(TOutput¤tElement,TKey¤tKey)
在System.Linq.Parallel.StopAndGoSpoolingTask`2.SpoolingWork()中
在System.Linq.Parallel.SpoolingTaskBase.Work()中
at System.Linq.Parallel.QueryTask.BaseWork(对象未使用)
在System.Linq.Parallel.QueryTask.c.b_uuuu10_0(对象o)中
在System.Threading.Tasks.Task.InnerInvoke()中
在System.Threading.Tasks.Task.Execute()中
内部异常:

不确定它为什么会发生以及如何解决。

问题似乎是您调用的ResizeImage方法。也许它不是线程安全的。您应该检查您对该方法的执行情况,或者您可以与我们共享该方法的执行情况


ps:我无法发表评论,这就是为什么我必须写作为答案。

我已经尝试了几种场景,使用您的代码+模拟大小调整:

1) 拍摄了大约700张随机位图-效果很好

2) 添加了一个非常大的位图(12K x 12K)-效果很好

3) 添加了一个伪位图(某些文本文件重命名为.bmp)-失败并返回错误

为了找出到底是哪一个给出了错误,可以使用以下代码:

public static Bitmap ResizeImage(Bitmap bmp, int width, int height)
{
    return bmp;
}

static void Main(string[] args)
{
    var imageslist = Directory.EnumerateFiles("C:\\BmpTest", "*.bmp", SearchOption.AllDirectories)
        .AsParallel()
        .Select(path =>
            {
                Bitmap ret = null;
                try
                {
                    ret = new Bitmap(path);
                }
                catch (Exception exc)
                {
                     // put breakpoint here and check path
                }
                return ret;
            })
        .Select(bmp => ResizeImage(bmp, 100, 100))
        .ToList();

什么是目录?是字符串吗?如果是,是哪个?显示初始化。异常列表已剪裁。你能发布一个完整的吗?根据堆栈跟踪,异常是由位图的构造函数引发的。另外,线程与此代码无关。是的,我没有注意到。这一点。在这种情况下,如果他有这么多png文件,那么这个异常可能与位图的内存分配有关。另外,在第二点上,你说这与线程无关。但我知道