public static void GetRGB(this Bitmap image, int startX, int startY, int w, int h, int[][] rgbArray) { const int PixelWidth = 3; const PixelFormat PixelFormat = PixelFormat.Format24bppRgb;
// En garde! if (image == null) throw new ArgumentNullException("image"); if (rgbArray == null) throw new ArgumentNullException("rgbArray"); if (startX < 0 || startX + w > image.Width) throw new ArgumentOutOfRangeException("startX"); if (startY < 0 || startY + h > image.Height) throw new ArgumentOutOfRangeException("startY"); if (w < 0 || w > image.Width) throw new ArgumentOutOfRangeException("w"); if (h < 0 || (rgbArray.Length < h) || h > image.Height) throw new ArgumentOutOfRangeException("h");
BitmapData data = image.LockBits(new Rectangle(startX, startY, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat); try { byte[] pixelData = new Byte[data.Stride]; for (int i = 0; i < h; i++) { rgbArray[i] = new int[w]; Marshal.Copy(data.Scan0 + (i * data.Stride), pixelData, 0, data.Stride); for (int j = 0; j < w; j++) { // PixelFormat.Format32bppRgb means the data is stored // in memory as BGR. We want RGB, so we must do some // bit-shuffling. rgbArray[i][j] = (pixelData[j * PixelWidth + 2] << 16) + // R (pixelData[j * PixelWidth + 1] << 8) + // G pixelData[j * PixelWidth]; // B } } } finally { image.UnlockBits(data); } } public static void GetRGBA(this Bitmap image, int startX, int startY, int w, int h, long[][] rgbArray) { const int PixelWidth = 4; const PixelFormat PixelFormat = PixelFormat.Format32bppArgb;
// En garde! if (image == null) throw new ArgumentNullException("image"); if (rgbArray == null) throw new ArgumentNullException("rgbArray"); if (startX < 0 || startX + w > image.Width) throw new ArgumentOutOfRangeException("startX"); if (startY < 0 || startY + h > image.Height) throw new ArgumentOutOfRangeException("startY"); if (w < 0 || w > image.Width) throw new ArgumentOutOfRangeException("w"); if (h < 0 || (rgbArray.Length < h) || h > image.Height) throw new ArgumentOutOfRangeException("h");
BitmapData data = image.LockBits(new Rectangle(startX, startY, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat); try { byte[] pixelData = new Byte[data.Stride]; for (int i = 0; i < h; i++) { rgbArray[i] = new long[w]; Marshal.Copy(data.Scan0 + (i * data.Stride), pixelData, 0, data.Stride); for (int j = 0; j < w; j++) { // PixelFormat.Format32bppARgb means the data is stored // in memory as BGRA. We want ARGB, so we must do some // bit-shuffling. rgbArray[i][j] = (((long)pixelData[j * PixelWidth + 3]) << 24) + // A (pixelData[j * PixelWidth + 2] << 16) + // R (pixelData[j * PixelWidth + 1] << 8) + // G pixelData[j * PixelWidth]; // B } } } finally { image.UnlockBits(data); } } public static void SetRGB(this Bitmap image, int startX, int startY, int w, int h, int[][] rgbArray) { const int PixelWidth = 3; const PixelFormat PixelFormat = PixelFormat.Format24bppRgb;
// En garde! if (image == null) throw new ArgumentNullException("image"); if (rgbArray == null) throw new ArgumentNullException("rgbArray"); if (startX < 0 || startX + w > image.Width) throw new ArgumentOutOfRangeException("startX"); if (startY < 0 || startY + h > image.Height) throw new ArgumentOutOfRangeException("startY"); if (w < 0 || w > image.Width) throw new ArgumentOutOfRangeException("w"); if (h < 0 || (rgbArray.Length < h) || h > image.Height) throw new ArgumentOutOfRangeException("h");
BitmapData data = image.LockBits(new Rectangle(startX, startY, w, h), System.Drawing.Imaging.ImageLockMode.WriteOnly, PixelFormat); try { int dst_bytes = data.Stride * h; byte[] dstValues = new byte[dst_bytes]; IntPtr dstPtr = data.Scan0; Marshal.Copy(dstPtr, dstValues, 0, dst_bytes); int position = 0;
for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { position = (i * data.Stride) + j * PixelWidth; dstValues[position + 0] = (byte)(rgbArray[i][j]); dstValues[position + 1] = (byte)(rgbArray[i][j] >> 8); dstValues[position + 2] = (byte)(rgbArray[i][j] >> 16); } } Marshal.Copy(dstValues, 0, dstPtr, dst_bytes); } finally { image.UnlockBits(data); } }
private Point FindTargetPoint(int[][] imgRGBArray) { //从500开始逐行扫描 RGB tempRGB; RGB tempBoxColorRGB = new RGB(); Point startPoint = new Point(0, 0); Point endPoint = new Point(0, 0); Point rightPoint = new Point(0, 0); int rightHeight = 0; int maybeHeight = 0; var isFindStart = false; var isInBox = true; var isFindRight = false; var findCount = 400;
var currentRowMaxJ = 0;
for (var i = 500; i < imgRGBArray.Length; i++) { currentRowMaxJ = 0; for (var j = 0; j < imgRGBArray[i].Length; j++) { tempRGB = RGB.FromInt(imgRGBArray[i][j]); if (!isFindStart) { if (!IsBgColor(tempRGB)) { isFindStart = true; startPoint.Y = i; startPoint.X = j; endPoint.X = j; rightPoint.X = j; tempBoxColorRGB = tempRGB; imgRGBArray[i][j] = (~tempRGB).ToInt(); currentRowMaxJ = j; } } else if (findCount >= 0) { if (RGB.FromInt(imgRGBArray[i][j]) == tempBoxColorRGB) { currentRowMaxJ = j;
if (!isFindRight) { imgRGBArray[i][j] = (~tempBoxColorRGB).ToInt(); } else if (isFindRight && (i - startPoint.Y) < (2 * maybeHeight + 20)) { imgRGBArray[i][j] = (~tempBoxColorRGB).ToInt(); endPoint.Y = i; } else if (isFindRight && (i - startPoint.Y) >= (2 * maybeHeight + 20)) { isInBox = false; break; } } } }
public JumpLog(DateTime shotScreenTime) { this.ShotScreenTime = shotScreenTime; }
public DateTime ShotScreenTime { get; set; } public DateTime AnalyseStartTime { get; set; } public DateTime AnalyseEndTime { get; set; } public int AnalyseTime { get { return (int)(AnalyseEndTime - AnalyseStartTime).TotalMilliseconds; } } public double AnalyseSpace { get; set; } public Point TopPointPosition { get; set; } public Point RightPointPosition { get; set; } public Point LeftPointPosition { get; set; } public Point CenterWhitePointPosition { get; set; } public JumpLogPointPositionType GetPointPositionType { get; set; } public Point TargetPointPosition { get; set; } public Point SelfChessPointPosition { get; set; } public int PressTime { get; set; } public string ScreenImageFileName { get; set; } public void AppendToFile(string filePath, string content) { using (var fileStream = File.Open(filePath, FileMode.Append, FileAccess.Write)) { var writeStream = new StreamWriter(fileStream, Encoding.UTF8); writeStream.WriteLine(string.Format("[{0}] : {1}", DateTime.Now.GetCommonDateString(), content)); writeStream.Flush(); writeStream.Close(); } } public void AppendToFile(string filePath) { AppendToFile(filePath, GetLogString()); } private string GetLogString() { return string.Format("[截图时间:{0}] [分析耗时:{1}ms] [分析最终距离:{2}px] [按压时间:{3}] [定位模式:{4}] [顶点坐标:{5}] [定位点坐标:{6}] [目标点坐标:{7}] [棋子坐标:{8}] [图片名称:{9}]", ShotScreenTime.GetCommonDateString(), AnalyseTime, AnalyseSpace.ToString("0.00"), PressTime, GetPointPositionType.GetDescription(), TopPointPosition.ToString(), GetRightAnchorPointPosition().ToString(), TargetPointPosition.ToString(), SelfChessPointPosition.ToString(), ScreenImageFileName); }
private Point GetRightAnchorPointPosition() { switch (GetPointPositionType) { case JumpLogPointPositionType.Center: return CenterWhitePointPosition; case JumpLogPointPositionType.Right: return RightPointPosition; case JumpLogPointPositionType.Left: return LeftPointPosition; } return new Point(-1, -1); } } public enum JumpLogPointPositionType { [Description("通过中心白点定位")] Center = 1, [Description("通过右端点定位")] Right = 2, [Description("通过左端点定位")] Left = 4, }
public static class StaticJumpLogPointPositionTypeHelp { public static string GetDescription(this JumpLogPointPositionType type) { FieldInfo EnumInfo = type.GetType().GetField(type.ToString()); DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo. GetCustomAttributes(typeof(DescriptionAttribute), false); if (EnumAttributes.Length > 0) { return EnumAttributes[0].Description; } return type.ToString(); } }