fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using System.Drawing.Imaging;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace BlitzBot
  11. {
  12. class BoardScraper
  13. {
  14. Bitmap bmpSrc;
  15. Bitmap bmpDst;
  16. Graphics gfx;
  17. Font fnt = new Font("Sans Serif", 12);
  18. Brush brsh = Brushes.Magenta;
  19. BitmapData bd;
  20. Rectangle r;
  21.  
  22. public BoardScraper()
  23. {
  24. InitializeBitmap();
  25. }
  26.  
  27. private void InitializeBitmap()
  28. {
  29. // Create bitmap
  30. //bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
  31. // Screen.PrimaryScreen.Bounds.Height,
  32. // PixelFormat.Format32bppArgb);
  33. bmpSrc = new Bitmap("tests/game1.png");
  34. bmpDst = new Bitmap(320, 316);
  35.  
  36. // Create a graphics object from the bitmap (so we can draw and shit on the bitmap)
  37. gfx = Graphics.FromImage(bmpSrc);
  38.  
  39. OutputColor(bmpSrc);
  40. }
  41.  
  42. public Bitmap NextBitmap()
  43. {
  44. // Take the screenshot from the upper left corner to the rightbottom corner.
  45. //gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
  46. // Screen.PrimaryScreen.Bounds.Y,
  47. // 0, 0, Screen.PrimaryScreen.Bounds.Size,
  48. // CopyPixelOperation.SourceCopy);
  49. //gfx.
  50.  
  51.  
  52.  
  53. return bmpSrc;
  54. }
  55.  
  56. private void OutputColor(Bitmap bm)
  57. {
  58. r = new Rectangle(1150, 410, 320, 316);
  59.  
  60. bd = bm.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  61.  
  62. // Get address of first line
  63. IntPtr ptr = bd.Scan0;
  64.  
  65. int bytes = Math.Abs(bd.Stride) * bm.Height;
  66. byte[] rgbValues = new byte[bytes];
  67.  
  68. // Copy the RGB values into the array
  69. Marshal.Copy(ptr, rgbValues, 0, bytes);
  70.  
  71. // Calculate average and set it.
  72. for (int y = r.Y; y < r.Y + r.Height; y += 38)
  73. {
  74. for (int x = r.X; x < r.X + r.Width; x += 40)
  75. {
  76. SetAverageColor(ref rgbValues, x, y);
  77. }
  78. }
  79.  
  80. // Copy the RGB values back to the bitmap
  81. Marshal.Copy(rgbValues, 0, ptr, bytes);
  82.  
  83. bm.UnlockBits(bd);
  84. }
  85.  
  86. private void SetAverageColor(ref byte[] rgb, int x, int y)
  87. {
  88. int width = r.Width;
  89. int height = r.Height;
  90.  
  91. int red = 0;
  92. int green = 0;
  93. int blue = 0;
  94.  
  95. int minDiversion = 15; // drop pixels that do not differ by at least minDiversion between color values (white, gray or black)
  96. int dropped = 0; // keep track of dropped pixels
  97.  
  98. long[] totals = new long[] { 0, 0, 0 };
  99. int bppModifier = bmpSrc.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? 3 : 4; // cutting corners, will fail on anything else but 32 and 24 bit images
  100.  
  101. int stride = bd.Stride;
  102.  
  103. for (int yy = y; yy < y + 38; yy++)
  104. {
  105. for (int xx = x; xx < x + 40; xx++)
  106. {
  107. int idx = (y * stride) + x * bppModifier;
  108. red = rgb[idx + 2];
  109. green = rgb[idx + 1];
  110. blue = rgb[idx];
  111. if (Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion)
  112. {
  113. totals[2] += red;
  114. totals[1] += green;
  115. totals[0] += blue;
  116. }
  117. else
  118. {
  119. dropped++;
  120. }
  121. }
  122. }
  123.  
  124. int count = width * height - dropped;
  125. int avgR = (int)(totals[2] / count);
  126. int avgG = (int)(totals[1] / count);
  127. int avgB = (int)(totals[0] / count);
  128.  
  129. for (int yy = y; yy < y + 38; yy++)
  130. {
  131. for (int xx = x; xx < x + 40; xx++)
  132. {
  133. int idx = (y * stride) + x * bppModifier;
  134. rgb[idx + 2] = (byte)avgR;
  135. rgb[idx + 1] = (byte)avgG;
  136. rgb[idx] = (byte)avgB;
  137.  
  138. }
  139. }
  140.  
  141. }
  142. }
  143. }
  144.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
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: 
compilation info
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
stdout
Standard output is empty