Iam new to the c# and Parallel processing. I am trying to process the bunch of images and i have written methods for processing.And I have added the Parallel foreach loop on imagelist as below
Parallel.ForEach(Directory.GetFiles(path), new ParallelOptions { MaxDegreeOfParallelism = 2 }, fileName =>
{
List<string> tempList = new List<string>();
using (Bitmap imageBitmap= new Bitmap(fileName))
{
using (ImageProcessor imageProcessor= new ImageProcessor ())
{
tempList = imageProcessor.ProcessImage(imageBitmap);
}
}
if (tempList.Count != 0)
allElements.Add(tempList.FirstOrDefault());
});
In one of the method I have used the LockBits
method to get the BitMapData
of the image and copying that data to the byte[]
but method is throwing the exception the code of method is
private byte[] ImageToByteArray(Bitmap b, out int stride)
{
object sync = new object();
BitmapData bd;
lock (sync)
{
Rectangle rect = new Rectangle(0, 0, b.Width, b.Height);
bd = b.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);//This line throwing the exception.
try
{
byte[] pxl = new byte[bd.Stride * b.Height];
Marshal.Copy(bd.Scan0, pxl, 0, bd.Stride * b.Height);
stride = bd.Stride;
return pxl;
}
finally
{
b.UnlockBits(bd);
}
}
}
I tried to lock
the code which is using the LockBits
method so at a time only one thread at a time use the code and hence the memory.
I also tried using the lock
with call of the methods from parent method like
public List<string> ProcessImage(BitMap image)
{
object sync = new object();
int stride;
byte[] rawImg;
using (image)
{
lock (sync)
{
rawImg = ImageToByteArray(image, out stride);
}
processingImg = new ProcessImage(rawImg, image.Width, image.Height);
}
}
but still the exception is there. The exception isn't give me any stack trace or explanation only Exception message I am getting is Out Of Memory
.
I came across this answer and when i reduced the MaximumDegreeOfParallelism
it worked correctly.
So basically I wanted to know,
1) why the code was throwing the Out Of Memory
exception for more that 2 threads even there is lock
on the code?
2) And is it possible to avoid this exception if I increase the DegreeOfParallelism
?
Any help would be great.
In case of any confusion feel free to comment.