I want to convert iPhone HEIC-images to Jpeg files. Since there are a lot of images, I handle this with Parallel.ForEach
string[] files = Directory.GetFiles(inputPath, "*.heic");
Parallel.ForEach(files, new ParallelOptions() { MaxDegreeOfParallelism = 16 }, file =>
{
using (var image = new MagickImage(file))
{
Console.WriteLine(file);
// Save frame as jpg
image.Write(Path.Combine(output, Path.GetFileNameWithoutExtension(file) + ".jpg"));
image.Dispose();
}
});
BUT when I set the MaxDegreeOfParallelism = 16
there comes this error (8 or below seems to work)
ImageMagick.MagickCorruptImageErrorException: Insufficient memory (case 4)
long:
Unbehandelte Ausnahme: System.AggregateException: Mindestens ein Fehler ist aufgetreten. ---> ImageMagick.MagickCorruptImageErrorException: Insufficient memory (case 4) `T:\Imageconversion\output\IMG_0934.jpg' @ error/jpeg.c/JPEGErrorHandler/343 bei ImageMagick.MagickExceptionHelper.Check(IntPtr exception) bei ImageMagick.MagickImage.NativeMagickImage.WriteFile(MagickSettings settings) bei ImageMagick.MagickImage.Write(String fileName) bei HEIC_Conversion.Program.<>c__DisplayClass2.<Main>b__1(String file) bei System.Threading.Tasks.Parallel.<>c__DisplayClass30_0`2.<ForEachWorker>b__0(Int32 i) bei System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1() bei System.Threading.Tasks.Task.InnerInvoke() bei System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask) bei System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object <p0>) --- Ende der internen Ausnahmestapelüberwachung --- bei System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) bei System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) bei System.Threading.Tasks.Task.Wait() bei System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally) bei System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](TSource[] array, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally) bei System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally) bei System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body)
Anyway there is enough memory left: (marked the exception with red arrow)