I have code that loading bitmap from file. And searching for black rectangle(5*5). Can you help me to review it?
namespace TakeScreen
{
class Program
{
static void Main(string[] args)
{
List<List<Color>> Sequence = new List<List<Color>>();
for (int i = 0; i < 5; i++)
{
List<Color> vector = new List<Color>();
for (int j = 0; j < 5; j++)
{
vector.Add(Color.Black);
}
Sequence.Add(vector);
}
TakeScreen TS = new TakeScreen();
Bitmap bitmap = TS.LoadImage(@"D:\ScreenShot.png");
List<Point> points = TS.FindPixelSequence(bitmap, Sequence);
}
}
public class TakeScreen
{
public Bitmap MakeScreenShot()
{
Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
using (Graphics g = Graphics.FromImage(bmpScreenCapture))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
bmpScreenCapture.Size,
CopyPixelOperation.SourceCopy);
}
return bmpScreenCapture;
}
public Bitmap LoadImage(string filePath)
{
Bitmap bmpScreenCapture = new Bitmap(filePath);
return bmpScreenCapture;
}
public void SaveScreenShot(Bitmap bitmap, string filePath)
{
bitmap.Save(filePath);
}
public List<Point> FindPixelSequence(Bitmap bitmap, List<List<Color>> soughForRectangele)
{
List<Point> Points = new List<Point>();
byte r0;
byte g0;
byte b0;
List<List<Color>> Rectangle = new List<List<Color>>();
List<Color> rectangleRow;
Stopwatch watch = new Stopwatch();
watch.Start();
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* ptrSrc = (byte*)data.Scan0;
for (int y = 0; y < data.Height; y = y + 1)
{
for (int x = 0; x < data.Width; x = x + 1)
{
Rectangle = new List<List<Color>>();
r0 = ptrSrc[2];
g0 = ptrSrc[1];
b0 = ptrSrc[0];
Color currentPixel = Color.FromArgb(0, r0, g0, b0);
if (currentPixel.R == soughForRectangele[0][0].R && currentPixel.G == soughForRectangele[0][0].G && currentPixel.B == soughForRectangele[0][0].B)
{
for (int j = 0; j < 5; j++)
{
rectangleRow = new List<Color>();
for (int i = 0; i < 5; i++)
{
r0 = ptrSrc[2];
g0 = ptrSrc[1];
b0 = ptrSrc[0];
rectangleRow.Add(Color.FromArgb(255, r0, g0, b0));
ptrSrc += 4;
}
ptrSrc -= (4 * 5);
Rectangle.Add(rectangleRow);
ptrSrc += (4 * data.Width);
}
ptrSrc -= (4 * data.Width) * 5;
if (CompareRectangels(soughForRectangele, Rectangle))
{
Points.Add(new Point { X = x, Y = y });
}
}
ptrSrc += 4;
}
}
}
bitmap.UnlockBits(data);
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
return Points;
}
public bool CompareRectangels(List<List<Color>> soughForRectangele, List<List<Color>> currentRectangele)
{
ClassqualityComparer C = new ClassqualityComparer();
List<bool> results = new List<bool>();
for (int i = 0; i < 5; i++)
{
results.Add(C.Equals(soughForRectangele[i], currentRectangele[i]));
}
if (results.Contains(false))
{
return false;
}
else
{
return true;
}
}
}
public class ClassqualityComparer : IEqualityComparer<List<Color>>
{
public bool Equals(List<Color> x, List<Color> y)
{
for (int i = 0; i < 5; i++)
{
if (x[i].R != y[i].R && x[i].G != y[i].G && x[i].B != y[i].B) return false;
}
return true;
}
public int GetHashCode(List<Color> obj)
{
throw new NotImplementedException();
}
}
}