public void Code(Bitmap N, int q) { // if (!sw.IsRunning) // { sw.Start(); // } New = N; int width = New.Width; int height = New.Height; int left = width; int right = 0; int top = height; int bottom = 0; List <Rectangle> horizantals = new List <Rectangle>(); Blocks = new List <Rectangle>(); BlocksData = new List <Stream>(); int lastX = -1, lastY = -1; if (Old == null || quality != q) { //if ther is no old image to compare to , just send the new one Old = New; quality = q; BlocksData.Add(CaptureScreen.Compress(New, q)); Blocks.Add(new Rectangle(0, 0, width, height)); } else { try { NewData = New.LockBits( new Rectangle(0, 0, New.Width, New.Height), ImageLockMode.ReadOnly, New.PixelFormat); OldData = Old.LockBits( new Rectangle(0, 0, Old.Width, Old.Height), ImageLockMode.ReadOnly, Old.PixelFormat); int strideNew = NewData.Stride / nbpp; int strideOld = OldData.Stride / nbpp; IntPtr scanNew0 = NewData.Scan0; IntPtr scanOld0 = OldData.Scan0; Console.WriteLine("setting up :" + sw.ElapsedMilliseconds + " ms"); sw.Reset(); sw.Start(); unsafe { int *pNew = (int *)(void *)scanNew0; int *pPrev = (int *)(void *)scanOld0; // we are going to scan from top to bottom , left to right // whene we finde something has changed we see if the position // of the that pixel if its the extremest , we set it as a border to get // the changed area for (int y = 0; y < New.Height; ++y) { for (int x = 0; x < Old.Width; ++x) { if ((pNew + x)[0] != (pPrev + x)[0]) { if (x < left) { left = x; } if (x > right) { right = x; } if (y < top) { top = y; } if (y > bottom) { bottom = y; } lastY = y; } } if ((y - lastY > 20 && lastY != -1) || (bottom == New.Height - 1 && top == 0)) { horizantals.Add(new Rectangle(left, top, right - left, bottom - top + 1)); left = width; right = 0; top = height; bottom = 0; lastY = -1; } pNew += strideNew; pPrev += strideOld; } Console.WriteLine("first scan :" + sw.ElapsedMilliseconds + " ms"); sw.Reset(); sw.Start(); //get small rectangles for (int i = 0; i < horizantals.Count; i++) { left = horizantals[i].X + horizantals[i].Width; right = horizantals[i].X; for (int x = horizantals[i].X; x < horizantals[i].X + horizantals[i].Width + 1; x++) { pNew = (int *)(void *)scanNew0; pPrev = (int *)(void *)scanOld0; for (int y = horizantals[i].Y; y < horizantals[i].Top + horizantals[i].Bottom; ++y) { if ((pNew + x)[0] != (pPrev + x)[0]) { if (x <= left) { left = x; } if (x >= right) { right = x; } lastX = x; } pNew += strideNew; pPrev += strideOld; } if ((x - lastX > 10 && lastX != -1) || (left == horizantals[i].X && right + 1 == horizantals[i].X + horizantals[i].Width + 1)) { Blocks.Add(new Rectangle(left, horizantals[i].Top, right - left, horizantals[i].Bottom - horizantals[i].Top)); left = horizantals[i].X + horizantals[i].Width; right = horizantals[i].X; lastX = -1; } } } Console.WriteLine("cutting :" + sw.ElapsedMilliseconds + " ms"); sw.Reset(); sw.Start(); } } catch (Exception ex) { } finally { if (NewData != null) { New.UnlockBits(NewData); } if (OldData != null) { Old.UnlockBits(OldData); } } for (int i = 0; i < Blocks.Count; i++) { try { Rectangle bounds = Blocks[i]; bounds.Width = bounds.Width == 0 ? 1 : bounds.Width = bounds.Width; bounds.Height = bounds.Width == 0 ? 1 : bounds.Height = bounds.Height; Bitmap diff = new Bitmap(bounds.Width, bounds.Height); Graphics _graphics = Graphics.FromImage(diff); if (bounds.Width > 0) { _graphics.DrawImage(New, 0, 0, bounds, GraphicsUnit.Pixel); } BlocksData.Add(CaptureScreen.Compress(diff, q)); _graphics.Flush(); _graphics.Dispose(); diff.Dispose(); } catch (Exception ex) { // Console.WriteLine(Blocks[i]); Console.WriteLine("code : " + ex.TargetSite + " | " + ex.Message); } } Console.WriteLine("creating pics :" + sw.ElapsedMilliseconds + " ms"); sw.Reset(); Old = New; } cou++; // if (sw.ElapsedMilliseconds >= 1000) // { // Console.WriteLine("frames :" + cou+ " per "+sw.ElapsedMilliseconds/1000+" secondes"); Console.WriteLine("--------------------------New lap ----------------------"); // sw.Reset(); // sw.Stop(); // cou = 0; // } }
public void Code(Bitmap N, int q) { BitmapData NewData = null; const int nbpp = 4; // number of bytes per pixel New = N; int width = New.Width; int height = New.Height; List <Rectangle> horizantals = new List <Rectangle>(); Blocks = new List <Rectangle>(); BlocksData = new List <Stream>(); try { NewData = New.LockBits( new Rectangle(0, 0, New.Width, New.Height), ImageLockMode.ReadOnly, New.PixelFormat); int strideNew = NewData.Stride / nbpp; IntPtr scanNew0 = NewData.Scan0; unsafe { byte *pScanNew0 = (byte *)scanNew0.ToInt32(); if (_Old == null || quality != q) { //if ther is no old image to compare to , just send the new one _Old = new byte[N.Width * N.Height * nbpp]; quality = q; BlocksData.Add(CaptureScreen.Compress(New, q)); Blocks.Add(new Rectangle(0, 0, width, height)); if (format == PixelFormat.DontCare) format = N.PixelFormat; fixed(byte *ptr = _Old) { NativeMethods.memcpy(new IntPtr(ptr), scanNew0, (uint)(N.Width * N.Height * nbpp)); } return; } fixed(byte *ptr = _Old) { int top = height; int bottom = 0; int lastY = -1; Rectangle cBlock = new Rectangle(); // we are going to cutt the image in horizontal pieces each time we finde something has changed for (int y = 0; y < New.Height; ++y) { int offset = (y * N.Width * nbpp); if (NativeMethods.memcmp(ptr + offset, pScanNew0 + offset, (uint)(N.Width * nbpp)) != 0) { if (y < top) { top = y; } if (y > bottom) { bottom = y; } lastY = y; NativeMethods.memcpy(ptr + offset, pScanNew0 + offset, (uint)(N.Width * nbpp)); } if ((y > lastY && lastY != -1) || (lastY == height - 1)) { Rectangle b = new Rectangle(0, top, N.Width, bottom - top + 1); horizantals.Add(b); top = height; bottom = 0; lastY = -1; } } } } } catch (Exception e) { Console.WriteLine("err : " + e.Message); } finally { if (NewData != null) { New.UnlockBits(NewData); } } // now we have a list of rectangles where changes has happened , so we are going to creat small images from each rectangle for (int i = 0; i < horizantals.Count; i++) { try { Rectangle bounds = horizantals[i]; bounds.Width = bounds.Width == 0 ? 1 : bounds.Width = bounds.Width; bounds.Height = bounds.Width == 0 ? 1 : bounds.Height = bounds.Height; Bitmap diff = new Bitmap(bounds.Width, bounds.Height); Graphics _graphics = Graphics.FromImage(diff); _graphics.DrawImage(New, 0, 0, bounds, GraphicsUnit.Pixel); // diff.Save("C:\\Users\\islam\\Desktop\\t\\" + Guid.NewGuid().ToString("N") + ".bmp"); BlocksData.Add(CaptureScreen.Compress(diff, q)); _graphics.Flush(); _graphics.Dispose(); diff.Dispose(); } catch (Exception e) { Console.WriteLine("adding exception : " + e.Message); } } Blocks = horizantals; }