HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Search rectangle on bitmap

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
rectanglesearchbitmap

Problem

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> Sequence = new List>();

for (int i = 0; i vector = new List();
for (int j = 0; j 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 FindPixelSequence(Bitmap bitmap, List> soughForRectangele)
{
List Points = new List();

byte r0;
byte g0;
byte b0;

List> Rectangle = new List>();
List 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 >();
r0 = ptrSrc

Solution

Just a few remarks ...


  1. Use "var" where it is possible. Code will be more readable. For example here



List> Rectangle = new List>();


  • Single responsibility principle is violated. Your class takes screenshot, load images, finds sequences ...



3.Rename "Sequence" to "blackRectangle" because it's quite difficult to understand from your code what you are doing. i understand it only because you wrote about black rectangle

4.On the first sight your outer loops can be cut off.

(int y = 0; y < data.Height)


I suppose this pseudo-code would be better

y<data.Height-soughForRectangele.Height

Code Snippets

List<List<Color>> Rectangle = new List<List<Color>>();
(int y = 0; y < data.Height)
y<data.Height-soughForRectangele.Height

Context

StackExchange Code Review Q#135171, answer score: 3

Revisions (0)

No revisions yet.