private void MatchByMaxDiff() { if (imageList.Count >= 2) { BitmapImage preImage = imageList[imageList.Count - 2]; BitmapImage curImage = imageList[imageList.Count - 1]; //获取上一次截图的像素点 byte[] prePixels = new byte[(int)preImage.PixelWidth * (int)preImage.PixelHeight * preImage.Format.BitsPerPixel / 8]; preImage.CopyPixels(prePixels, (int)(preImage.PixelWidth * preImage.Format.BitsPerPixel / 8), 0); //获取本次截图的像素点 byte[] curPixels = new byte[(int)curImage.PixelWidth * (int)curImage.PixelHeight * curImage.Format.BitsPerPixel / 8]; curImage.CopyPixels(curPixels, (int)(curImage.PixelWidth * curImage.Format.BitsPerPixel / 8), 0); int matchRowCount = 100; int[] diffVlaues = new int[preImage.PixelWidth]; int pixbytes = preImage.Format.BitsPerPixel / 8; int oneRowBytesCount = preImage.PixelWidth * pixbytes; //行 for (int i = 0; i < matchRowCount && i < preImage.PixelHeight - 1; i++) { //列 for (int j = 0; j < preImage.PixelWidth; j++) { int startIndex = prePixels.Length - (i + 1) * oneRowBytesCount + j * pixbytes; diffVlaues[j] += Math.Abs(prePixels[startIndex] - prePixels[startIndex - oneRowBytesCount]) + Math.Abs(prePixels[startIndex + 1] - prePixels[startIndex + 1 - oneRowBytesCount]) + Math.Abs(prePixels[startIndex + 2] - prePixels[startIndex + 2 - oneRowBytesCount]); } } //寻找差异化最大的一列 int colIndex = diffVlaues.ToList().IndexOf(diffVlaues.Max()); List <byte> colValues = new List <byte>(); int startRowIndex = preImage.PixelHeight > matchRowCount ? preImage.PixelHeight - matchRowCount : 0; for (int row = startRowIndex; row < preImage.PixelHeight; row++) { int startIndex = row * oneRowBytesCount + colIndex * pixbytes; colValues.Add(prePixels[startIndex]); colValues.Add(prePixels[startIndex + 1]); colValues.Add(prePixels[startIndex + 2]); } //对比差异化最大的一列 List <byte> curColValues = new List <byte>(); int curPixbytes = curImage.Format.BitsPerPixel / 8; int curOneRowBytesCount = curImage.PixelWidth * curPixbytes; for (int row = 0; row < curImage.PixelHeight; row++) { int startIndex = row * curOneRowBytesCount + colIndex * curPixbytes; curColValues.Add(curPixels[startIndex]); curColValues.Add(curPixels[startIndex + 1]); curColValues.Add(curPixels[startIndex + 2]); } bool isMatch = false; int matchRow = 0; int errorCount = 0; for (int i = errorCount * 3, j = 0; i < curColValues.Count - 2 && j < colValues.Count - 2; i += 3, j += 3) { if (curColValues[i] == colValues[j] && curColValues[i + 1] == colValues[j + 1] && curColValues[i + 2] == colValues[j + 2]) { if (j == colValues.Count - 3) { isMatch = true; matchRow = i / 3; break; } } else { i = errorCount * 3; errorCount += 1; j = -3; } } if (isMatch) { curPixels = curPixels.Skip((matchRow) * (int)preImage.PixelWidth * preImage.Format.BitsPerPixel / 8).ToArray(); WriteableBitmap bitmap = new WriteableBitmap(curImage.PixelWidth, curImage.PixelHeight - matchRow, 96, 96, curImage.Format, null); bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), curPixels, (int)curImage.PixelWidth * curImage.Format.BitsPerPixel / 8, 0); imageList.Remove(curImage); imageList.Add(ConvertHelper.ToBitmapImage(bitmap)); } } }
private void MatchByComparePix() { int matchedRow = 0; if (imageList.Count >= 2) { BitmapImage preImage = imageList[imageList.Count - 2]; BitmapImage curImage = imageList[imageList.Count - 1]; //获取上一次截图的像素点 byte[] prePixels = new byte[(int)preImage.PixelWidth * (int)preImage.PixelHeight * preImage.Format.BitsPerPixel / 8]; preImage.CopyPixels(prePixels, (int)(preImage.PixelWidth * preImage.Format.BitsPerPixel / 8), 0); //获取本次截图的像素点 byte[] curPixels = new byte[(int)curImage.PixelWidth * (int)curImage.PixelHeight * curImage.Format.BitsPerPixel / 8]; curImage.CopyPixels(curPixels, (int)(curImage.PixelWidth * curImage.Format.BitsPerPixel / 8), 0); bool isMatchAllCol = true; int startRow = curImage.PixelHeight / 3 * 2; for (; startRow >= 0; startRow--) { int preRow = preImage.PixelHeight - 1; int curRow = startRow; isMatchAllCol = true; int minNum = 10; int num = 0; for (; num < 30 && preRow >= 0 && curRow >= 0; preRow--, curRow--, num++) { int col = 0; int difCount = 0; for (; col < preImage.PixelWidth; col += 2) { int preIndex = preRow * curImage.PixelWidth * curImage.Format.BitsPerPixel / 8 + col * curImage.Format.BitsPerPixel / 8; int curIndex = curRow * curImage.PixelWidth * curImage.Format.BitsPerPixel / 8 + col * curImage.Format.BitsPerPixel / 8; if (prePixels[preIndex] != curPixels[curIndex] || prePixels[preIndex + 1] != curPixels[curIndex + 1] || prePixels[preIndex + 2] != curPixels[curIndex + 2]) { difCount++; if (difCount > 10) { isMatchAllCol = false; break; } } } if (!isMatchAllCol) { break; } } if (isMatchAllCol) { if (num < minNum) { isMatchAllCol = false; } break; } } if (isMatchAllCol) { matchedRow = startRow; curPixels = curPixels.Skip((matchedRow + 1) * (int)preImage.PixelWidth * preImage.Format.BitsPerPixel / 8).ToArray(); WriteableBitmap bitmap = new WriteableBitmap(curImage.PixelWidth, curImage.PixelHeight - startRow - 1, 96, 96, curImage.Format, null); bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), curPixels, (int)curImage.PixelWidth * curImage.Format.BitsPerPixel / 8, 0); imageList.Remove(curImage); imageList.Add(ConvertHelper.ToBitmapImage(bitmap)); } } }