public void FindDifferences(bool hotRegionScan, Rectangle rect) { Bitmap nBit = ScreenCaptureHelper.CaptureNoCursor(); if (!hotRegionScan) { nBit = ScreenCaptureHelper.SizeImage(nBit, rect.Width, rect.Height); } if (_m_Oldbmp.Height != nBit.Height || _m_Oldbmp.Width != nBit.Width) { _m_Oldbmp.Dispose(); _m_Oldbmp = new Bitmap(nBit.Width, nBit.Height); } int newBitHeight = nBit.Height / _row; int newBitWidth = nBit.Width / _column; //余 int surplusHeight = nBit.Height % _row; int surplusWidth = nBit.Width % _column; List <byte> Buffer = new List <byte>(); try { int y = 0; for (int i = 0; i < _row; i++) { int x = 0; for (int j = 0; j < _column; j++) { //计算是否撞热区域 int hotRegionX = rect.X + rect.Width; int hotRegionY = rect.Y + rect.Height; bool result = x >= (rect.X - newBitWidth) && x <= hotRegionX && y >= (rect.Y - newBitHeight) && y <= hotRegionY; if (result || !hotRegionScan) { int sw = 0; int sh = 0; if ((i + 1) == _row) { sh = surplusHeight; } if ((j + 1) == _column) { sw = surplusWidth; } int cloneWidth = newBitWidth + sw; int cloneHeight = newBitHeight + sh; Bitmap m_new = nBit.Clone( new Rectangle(x, y, cloneWidth, cloneHeight), _format); Bitmap m_old = _m_Oldbmp.Clone( new Rectangle(x, y, cloneWidth, cloneHeight), _format); bool isEqually = BitmapComprae(m_new, m_old); bool isHotRectChanged = hotRegionScan ? (rect.X != _clientHotRegion.X || rect.Y != _clientHotRegion.Y || rect.Width != _clientHotRegion.Width || rect.Height != _clientHotRegion.Height) : false; if (isEqually || isHotRectChanged) { using (MemoryStream ms = new MemoryStream()) { m_new.Save(ms, ImageFormat.Jpeg); var fragments = new Fragment[] { new Fragment() { X = x, Y = y, Height = cloneHeight, Width = cloneWidth, FragmentData = ms.ToArray() } }; this.OnDifferencesNotice?.Invoke(fragments, DifferStatus.NEXTSCREEN); } } else { m_old.Dispose(); } } x += newBitWidth; } y += newBitHeight; } } catch { } _clientHotRegion = rect; this.OnDifferencesNotice?.Invoke(null, DifferStatus.COMPLETE); if (_m_Oldbmp != null) { _m_Oldbmp.Dispose(); } _m_Oldbmp = nBit; }
public ScreenSpy() { var map = ScreenCaptureHelper.CaptureNoCursor(); _m_Oldbmp = new Bitmap(map.Width, map.Height); }