private static bool IsHorizontalLineWithColor(LockedBitmapData data, int y, Color color, IColorComparer comparer) { for (int x = 0; x < data.Bitmap.Width; x++) { if (!comparer.Compare(data.GetPixel(x, y), color)) { return(false); } } return(true); }
/// <summary> /// Applies a transformation to each pixel of a bitmap. /// </summary> public static void TransformPixels(Bitmap bitmap, Func <Color, Color> transformFunc) { using (var data = new LockedBitmapData(bitmap, writeable: true)) { for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { data.SetPixel(x, y, transformFunc(data.GetPixel(x, y))); } } } }
private static int GetVerticalLineLength(LockedBitmapData data, int x, int startY, Func <Color, bool> predicate) { int y; for (y = startY; y < data.Bitmap.Height; y++) { if (!predicate(data.GetPixel(x, y))) { break; } } return(y - startY); }
/// <summary> /// Same as FindFirstPoint but the search begins at the bottom right and proceeds backwards. /// </summary> public static Point?FindLastPoint(Bitmap bitmap, Rectangle searchRect, Func <Color, bool> predicate) { using (var data = new LockedBitmapData(bitmap)) { for (int y = searchRect.Bottom - 1; y >= searchRect.Top; y--) { for (int x = searchRect.Right - 1; x >= searchRect.Left; x--) { if (predicate(data.GetPixel(x, y))) { return(new Point(x, y)); } } } } return(null); }
private static int CalculateBitmapRectDifference(LockedBitmapData data1, Point offset1, LockedBitmapData data2, Point offset2, Size size, IColorComparer comparer, int?maxDifference = null) { int diffs = 0; for (int y = 0; y < size.Height; y++) { for (int x = 0; x < size.Width; x++) { var col1 = data1.GetPixel(x + offset1.X, y + offset1.Y); var col2 = data2.GetPixel(x + offset2.X, y + offset2.Y); if (!comparer.Compare(col1, col2)) { diffs++; if (maxDifference.HasValue && diffs > maxDifference.Value) { return(diffs); } } } } return(diffs); }