Rotate Images in a Parallel For (ImageMagick.MagickResourceLimitErrorException)
Posted: 2018-09-07T12:35:21-07:00
Hello Everyone.
I"ve been trying to rotate 5 images simultaneoulsy, using a Parallel.For
However, i'm always having a Magick.OutOfResource (something like it) exception.
Is there anything i can do to change the max memory MagickNET can allocate ?
This is the code that shows the problem:
And this is the ImageRotate Class:
The images are 5 20MB tiff images.
Thanks in advance for any help.
EDIT3:
Tried to set:
No results - same problem.
EDIT2: Tried to do the same thing using a regular loop and had the same error.
EDIT: Exception Details
ImageMagick.MagickResourceLimitErrorException
HResult=0x80131500
Message=memory allocation failed `' @ error/tiff.c/ReadTIFFImage/1773
Source=Magick.NET-Q8-x86
StackTrace:
at ImageMagick.NativeInstance.CheckException(IntPtr exception, IntPtr result)
at ImageMagick.MagickImage.NativeMagickImage.ReadBlob(MagickSettings settings, Byte[] data, Int32 length)
at ImageMagick.MagickImage.Read(Byte[] data, Int32 length, MagickReadSettings readSettings, Boolean ping)
at ImageMagick.MagickImage.Read(Byte[] data, MagickReadSettings readSettings)
at ImageMagick.MagickImage..ctor(Byte[] data)
at DI_Plot_2018.ImageProcessing.RotateImage..ctor(Byte[] BytesToRotate, Int32 RotationIndex) in C:\Users\Pablo\source\repos\DI-Plot 2018\ImageProcessing\RotateImage.cs:line 16
at DI_Plot_2018.StartProcessing.BeginProcessKeyValuePair.<>c__DisplayClass2_0.<RotatedImages>b__0(Int32 i) in C:\Users\Pablo\source\repos\DI-Plot 2018\StartProcessing\BeginProcessKeyValuePair.cs:line 45
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
I"ve been trying to rotate 5 images simultaneoulsy, using a Parallel.For
However, i'm always having a Magick.OutOfResource (something like it) exception.
Is there anything i can do to change the max memory MagickNET can allocate ?
This is the code that shows the problem:
Code: Select all
Parallel.For(0, ImagesToRotate.Count, i =>
{
MagickImage test = new MagickImage(ImagesToRotate.ElementAt(i).Value);
var ImgRotated = new ImageProcessing.RotateImage(test, RotationIndex).BytesFromRotatedImage;
//Inside RotateImage a MagickImage object is created
RotatedStuff.Add(ImagesToRotate.ElementAt(i).Key, ImgRotated);
//Images.Add(ImgRotated.rotatedImage);
});
And this is the ImageRotate Class:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;
namespace DI_Plot_2018.ImageProcessing
{
class RotateImage
{
public MagickImage rotatedImage { get; private set; }
public byte[] BytesFromRotatedImage { get; private set; }
public RotateImage(byte[] BytesToRotate, int RotationIndex)
{
using (MagickImage img = new MagickImage(BytesToRotate))
{
switch (RotationIndex)
{
case 0:
{
rotatedImage = img;
break;
}
case 1:
{
img.Rotate(90);
break;
}
case 2:
{
img.Rotate(180);
break;
}
case 3:
{
img.Rotate(270);
break;
}
case 4:
{
img.Flip();
img.Rotate(90);
break;
}
case 5:
{
img.Flip();
img.Rotate(180);
break;
}
case 6:
{
img.Flip();
img.Rotate(270);
break;
}
case 7:
{
RotationIndex = 0;
break;
}
}
rotatedImage = img;
}
}
}
}
Thanks in advance for any help.
EDIT3:
Tried to set:
Code: Select all
ResourceLimits.Memory = 2147483648; //2GB
ResourceLimits.Thread = 20;
EDIT2: Tried to do the same thing using a regular loop and had the same error.
EDIT: Exception Details
ImageMagick.MagickResourceLimitErrorException
HResult=0x80131500
Message=memory allocation failed `' @ error/tiff.c/ReadTIFFImage/1773
Source=Magick.NET-Q8-x86
StackTrace:
at ImageMagick.NativeInstance.CheckException(IntPtr exception, IntPtr result)
at ImageMagick.MagickImage.NativeMagickImage.ReadBlob(MagickSettings settings, Byte[] data, Int32 length)
at ImageMagick.MagickImage.Read(Byte[] data, Int32 length, MagickReadSettings readSettings, Boolean ping)
at ImageMagick.MagickImage.Read(Byte[] data, MagickReadSettings readSettings)
at ImageMagick.MagickImage..ctor(Byte[] data)
at DI_Plot_2018.ImageProcessing.RotateImage..ctor(Byte[] BytesToRotate, Int32 RotationIndex) in C:\Users\Pablo\source\repos\DI-Plot 2018\ImageProcessing\RotateImage.cs:line 16
at DI_Plot_2018.StartProcessing.BeginProcessKeyValuePair.<>c__DisplayClass2_0.<RotatedImages>b__0(Int32 i) in C:\Users\Pablo\source\repos\DI-Plot 2018\StartProcessing\BeginProcessKeyValuePair.cs:line 45
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()