我正在更改我的代码以使用Parallel.For
而不是简单的for
语句。我的代码有3个嵌套for
,可以逐个像素地对两个图像进行比较。
在最后for
我保存了切割点(TopLeft和BottomRight),应该剪切图像以减少文件大小。
//For the 1st way:
int firstY = listBit[index].Height; //starts from the end.
int firstX = listBit[index].Width;
int lastY = 0; int lastX = 0;
//For the 2nd way:
var valueX = new List<int>();
var valueY = new List<int>();
//For the 3rd way:
var startY = new bool[listBit[index].Height];
var startX = new bool[listBit[index].Width];
//x - width - sides
Parallel.For(0, width, x =>
{
//y - height - up/down
for (int y = 0; y < height; y++)
{
//image1 and image2 are bitmaps (inside a help class).
if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
{
image2.SetPixel(x, y, transparent);
}
else
{
//Here i save the cut points, to crop the image later.
//Old way: (compare and store in a variable)
if (x < firstX)
{
firstX = x;
} //here was the rest of it, works only with a sequencial loop.
//2nd way: (store in a list all entries, the List<> will blow, eventually)
valueX.Add(x);
valueY.Add(y); //and later use LINQ to get the values (first and last).
//3rd way: (store a flag for each pixel, works)
startX[x] = true;
startY[y] = true;
//but how can I get the first and last values==true of this array?
//Any other idea?
}
}
});
获得切入点的更好方法是什么?