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; } } } } }
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)
at System.Runtime.InteropServices.Marshal.Copy(IntPtr source, Byte[] destination, Int32 startIndex, Int32 length)
at BlitzBot.BoardScraper.OutputColor(Bitmap bm) in C:\Users\zol\Documents\Visual Studio 2010\Projects\C#\BlitzBot\BlitzBot\BoardScraper.cs:line 69
at BlitzBot.BoardScraper.InitializeBitmap() in C:\Users\zol\Documents\Visual Studio 2010\Projects\C#\BlitzBot\BlitzBot\BoardScraper.cs:line 39
at BlitzBot.BoardScraper..ctor() in C:\Users\zol\Documents\Visual Studio 2010\Projects\C#\BlitzBot\BlitzBot\BoardScraper.cs:line 24
at BlitzBot.MainForm..ctor() in C:\Users\zol\Documents\Visual Studio 2010\Projects\C#\BlitzBot\BlitzBot\MainForm.cs:line 20
at BlitzBot.Program.Main() in C:\Users\zol\Documents\Visual Studio 2010\Projects\C#\BlitzBot\BlitzBot\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
prog.cs(5,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference? prog.cs(6,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference? prog.cs(7,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference? prog.cs(15,9): error CS0246: The type or namespace name `Bitmap' could not be found. Are you missing a using directive or an assembly reference? prog.cs(16,9): error CS0246: The type or namespace name `Graphics' could not be found. Are you missing a using directive or an assembly reference? prog.cs(17,9): error CS0246: The type or namespace name `Font' could not be found. Are you missing a using directive or an assembly reference? prog.cs(18,9): error CS0246: The type or namespace name `Brush' could not be found. Are you missing a using directive or an assembly reference? prog.cs(19,9): error CS0246: The type or namespace name `BitmapData' could not be found. Are you missing a using directive or an assembly reference? prog.cs(20,9): error CS0246: The type or namespace name `Rectangle' could not be found. Are you missing a using directive or an assembly reference? prog.cs(42,16): error CS0246: The type or namespace name `Bitmap' could not be found. Are you missing a using directive or an assembly reference? prog.cs(56,34): error CS0246: The type or namespace name `Bitmap' could not be found. Are you missing a using directive or an assembly reference? prog.cs(5,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference? prog.cs(6,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference? prog.cs(7,14): error CS0234: The type or namespace name `Drawing' does not exist in the namespace `System'. Are you missing an assembly reference? Compilation failed: 14 error(s), 0 warnings
Standard output is empty