Parallel.For(0, Height, new ParallelOptions { MaxDegreeOfParallelism = 6 }, y =>
{
int currentLine = y * BMPData.Stride;
for (int x = 0; x < Width; x = x + BPX)
{
var b = pixels[currentLine + x];
var g = pixels[currentLine + x + 1];
var r = pixels[currentLine + x + 2];
int avg = (r + g + b) / 3;
pixels[currentLine + x] = (byte)avg;
pixels[currentLine + x + 1] = (byte)avg;
pixels[currentLine + x + 2] = (byte)avg;
}
});
This is basically a parallel code where it turns bitmap data pixels into gray ones, but is there any way I can replace the Parallel.For
usage with Parallel Linq? I know there's no point to this, but it's required for a certain assignment
You can use Enumerable.Range
, and ForAll
to get similar results. Although note ForAll
will not process the list in any stable order - which is probably fine for your use case
Enumerable.Range(0,Height)
.AsParallel()
.WithDegreeOfParallelism(6)
.ForAll(y =>
{
/// existing code here
});