private static Bitmap PixelateLockBitsParallel(Bitmap image, Rectangle rectangle, int pixelateSize) { using (LockBitmap lockBitmap = new LockBitmap(image)) { var width = image.Width; var height = image.Height; for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize) { for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize) { Int32 offsetX = pixelateSize / 2; Int32 offsetY = pixelateSize / 2; // make sure that the offset is within the boundry of the image while (xx + offsetX >= image.Width) { offsetX--; } while (yy + offsetY >= image.Height) { offsetY--; } // get the pixel color in the center of the soon to be pixelated area Color pixel = lockBitmap.GetPixel(xx + offsetX, yy + offsetY); // for each pixel in the pixelate size, set it to the center color Parallel.For(xx, xx + pixelateSize, x => { if (x < width) { Parallel.For(yy, yy + pixelateSize, y => { if (y < height) { lockBitmap.SetPixel(x, y, pixel); } }); } }); } } } return(image); }
private static Bitmap PixelateLockBitsParallel(Bitmap image, Rectangle rectangle, int pixelateSize) { using (LockBitmap lockBitmap = new LockBitmap(image)) { var width = image.Width; var height = image.Height; for (Int32 xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize) { for (Int32 yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize) { Int32 offsetX = pixelateSize / 2; Int32 offsetY = pixelateSize / 2; // make sure that the offset is within the boundry of the image while (xx + offsetX >= image.Width) offsetX--; while (yy + offsetY >= image.Height) offsetY--; // get the pixel color in the center of the soon to be pixelated area Color pixel = lockBitmap.GetPixel(xx + offsetX, yy + offsetY); // for each pixel in the pixelate size, set it to the center color Parallel.For(xx, xx + pixelateSize, x => { if (x < width) { Parallel.For(yy, yy + pixelateSize, y => { if (y < height) { lockBitmap.SetPixel(x, y, pixel); } }); } }); } } } return image; }