using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace BlitzBot { class BoardScraper { Bitmap bmpSrc; Bitmap bmpDst; Graphics gfx; Font fnt = new Font("Sans Serif", 12); Brush brsh = Brushes.Magenta; BitmapData bd; Rectangle r; public BoardScraper() { InitializeBitmap(); } private void InitializeBitmap() { // Create bitmap //bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, // Screen.PrimaryScreen.Bounds.Height, // PixelFormat.Format32bppArgb); bmpSrc = new Bitmap("tests/game1.png"); bmpDst = new Bitmap(320, 316); // Create a graphics object from the bitmap (so we can draw and shit on the bitmap) gfx = Graphics.FromImage(bmpSrc); OutputColor(bmpSrc); } public Bitmap NextBitmap() { // Take the screenshot from the upper left corner to the rightbottom corner. //gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, // Screen.PrimaryScreen.Bounds.Y, // 0, 0, Screen.PrimaryScreen.Bounds.Size, // CopyPixelOperation.SourceCopy); //gfx. return bmpSrc; } private void OutputColor(Bitmap bm) { r = new Rectangle(1150, 410, 320, 316); bd = bm.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // Get address of first line IntPtr ptr = bd.Scan0; int bytes = Math.Abs(bd.Stride) * bm.Height; byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array Marshal.Copy(ptr, rgbValues, 0, bytes); // Calculate average and set it. for (int y = r.Y; y < r.Y + r.Height; y += 38) { for (int x = r.X; x < r.X + r.Width; x += 40) { SetAverageColor(ref rgbValues, x, y); } } // Copy the RGB values back to the bitmap Marshal.Copy(rgbValues, 0, ptr, bytes); bm.UnlockBits(bd); } private void SetAverageColor(ref byte[] rgb, int x, int y) { int width = r.Width; int height = r.Height; int red = 0; int green = 0; int blue = 0; int minDiversion = 15; // drop pixels that do not differ by at least minDiversion between color values (white, gray or black) int dropped = 0; // keep track of dropped pixels long[] totals = new long[] { 0, 0, 0 }; int bppModifier = bmpSrc.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? 3 : 4; // cutting corners, will fail on anything else but 32 and 24 bit images int stride = bd.Stride; for (int yy = y; yy < y + 38; yy++) { for (int xx = x; xx < x + 40; xx++) { int idx = (y * stride) + x * bppModifier; red = rgb[idx + 2]; green = rgb[idx + 1]; blue = rgb[idx]; if (Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion) { totals[2] += red; totals[1] += green; totals[0] += blue; } else { dropped++; } } } int count = width * height - dropped; int avgR = (int)(totals[2] / count); int avgG = (int)(totals[1] / count); int avgB = (int)(totals[0] / count); for (int yy = y; yy < y + 38; yy++) { for (int xx = x; xx < x + 40; xx++) { int idx = (y * stride) + x * bppModifier; rgb[idx + 2] = (byte)avgR; rgb[idx + 1] = (byte)avgG; rgb[idx] = (byte)avgB; } } } } }