private void _Start() { while(_Running) { try { if(!Capturing) { Thread.Sleep(10); continue; } System.Diagnostics.Stopwatch SW = new Stopwatch(); SW.Start(); _MouseCapture.Update(); if((DateTime.Now - LastScreenUpdate).TotalMilliseconds > _Intervalms) { //prevent excessive checking if(_RunningAsService && (DateTime.Now - LastDeskSwitch).TotalMilliseconds > 500) {//only a program running as under the account nt authority\system is allowed to switch desktops var d = _DesktopInfo.GetActiveDesktop(); _ScreenCapture.ReleaseHandles();//make sure to release any handles before switching if(d != _DesktopInfo.Current_Desktop) { _DesktopInfo.SwitchDesktop(d); } LastDeskSwitch = DateTime.Now; } SW.Stop(); var img = _ScreenCapture.GetScreen(new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); Debug.WriteLine("GetScreen time: " + SW.ElapsedMilliseconds +"ms"); SW.Start(); Rectangle rect = Desktop_Sharing_Shared.Bitmap_Helper.Get_Diff(_LastImage, img); _LastImage = img; if(rect.Width > 0 && rect.Height > 0) { var rawimg = new Raw_Image { Data = Desktop_Sharing_Shared.Bitmap_Helper.Copy(img, rect), Dimensions = rect }; SW.Stop(); Debug.WriteLine("GetScreen Copy time: " + SW.ElapsedMilliseconds + "ms"); ScreenScanEvent.WaitOne(); System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(ScreenUpdate_ThreadProc), rawimg); } LastScreenUpdate = DateTime.Now; } lock(_InputLock) { foreach(var item in _KeyboardEvents) _DesktopInfo.InputKeyEvent(item); foreach(var item in _MouseEvents) _DesktopInfo.InputMouseEvent(item); _KeyboardEvents.Clear(); _MouseEvents.Clear(); } Thread.Sleep(10); } catch(Exception e) { Console.WriteLine(e.ToString()); } } }
public void Stop() { _Running = false; //if(_LastImage != null) // _LastImage.Dispose(); _LastImage = null; if(_DesktopInfo != null) _DesktopInfo.Dispose(); _DesktopInfo = null; if(_MouseCapture != null) _MouseCapture.Dispose(); _MouseCapture = null; if(_ScreenCapture != null) _ScreenCapture.Dispose(); _ScreenCapture = null; }
public Raw_Image GetScreen(Size sz) { IntPtr hOldBmp = IntPtr.Zero; Raw_Image raw = new Raw_Image(); try { var dt = DateTime.Now; if (nDesk == IntPtr.Zero) { nDesk = PInvoke.GetDesktopWindow(); } if (nSrce == IntPtr.Zero) { nSrce = PInvoke.GetWindowDC(nDesk); } if (nDest == IntPtr.Zero) { nDest = PInvoke.CreateCompatibleDC(nSrce); } if (nBmp == IntPtr.Zero) { nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); } else if (sz.Height != PreviousSize.Height || sz.Width != PreviousSize.Width) {// user changed resolution.. get new bitmap PInvoke.DeleteObject(nBmp); nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); } PreviousSize = sz; hOldBmp = PInvoke.SelectObject(nDest, nBmp); bool b = PInvoke.BitBlt(nDest, 0, 0, sz.Width, sz.Height, nSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Desktop_Sharing_Shared.Screen.PInvoke.BITMAPINFO bi = new PInvoke.BITMAPINFO(); bi.bmiHeader.biSize = 40; bi.bmiHeader.biWidth = sz.Width; bi.bmiHeader.biHeight = -sz.Height; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = Desktop_Sharing_Shared.Screen.PInvoke.BitmapCompressionMode.BI_RGB; bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; var dwBmpSize = ((sz.Width * bi.bmiHeader.biBitCount + 31) / 32) * 4 * sz.Height; raw.Data = new byte[dwBmpSize]; raw.Dimensions = new Rectangle(0, 0, sz.Width, sz.Height); Desktop_Sharing_Shared.Screen.PInvoke.GetDIBits(nSrce, nBmp, 0, Convert.ToUInt32(sz.Height), raw.Data, ref bi, PInvoke.DIB_Color_Mode.DIB_RGB_COLORS); //unsafe //{ // fixed(byte* datb = raw.Data) // { // using(Bitmap image = new Bitmap(sz.Width, sz.Height, sz.Width * 4, // PixelFormat.Format32bppRgb, new IntPtr(datb))) // { // image.Save(@"C:\Users\scott\Desktop\text.png", ImageFormat.Png); // } // } //} //Bitmap bmp = Bitmap.FromHbitmap(nBmp); PInvoke.SelectObject(nDest, hOldBmp); // return bmp; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } // return new Bitmap(sz.Width, sz.Height); return(raw); }
public void Start() { _Running = true; _ScreenCapture = new Desktop_Sharing_Shared.Screen.ScreenCapture(80); _KeyboardEvents = new List<Desktop_Sharing_Shared.Keyboard.KeyboardEventStruct>(); _MouseEvents = new List<Desktop_Sharing_Shared.Mouse.MouseEventStruct>(); _DesktopInfo = new Desktop_Sharing_Shared.Desktop.DesktopInfo(); _MouseCapture = new Desktop_Sharing_Shared.Mouse.MouseCapture(); _MouseCapture.MouseImageChangedEvent += _MouseCapture_MouseImageChangedEvent; _MouseCapture.MousePositionChangedEvent += _MouseCapture_MousePositionChangedEvent; _LastImage = _ScreenCapture.GetScreen(new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); _Start(); }
public Raw_Image GetScreen(Size sz) { IntPtr hOldBmp = IntPtr.Zero; Raw_Image raw = new Raw_Image(); try { var dt = DateTime.Now; if(nDesk == IntPtr.Zero) nDesk = PInvoke.GetDesktopWindow(); if(nSrce == IntPtr.Zero) nSrce = PInvoke.GetWindowDC(nDesk); if(nDest == IntPtr.Zero) nDest = PInvoke.CreateCompatibleDC(nSrce); if(nBmp == IntPtr.Zero) nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); else if(sz.Height != PreviousSize.Height || sz.Width != PreviousSize.Width) {// user changed resolution.. get new bitmap PInvoke.DeleteObject(nBmp); nBmp = PInvoke.CreateCompatibleBitmap(nSrce, sz.Width, sz.Height); } PreviousSize = sz; hOldBmp = PInvoke.SelectObject(nDest, nBmp); bool b = PInvoke.BitBlt(nDest, 0, 0, sz.Width, sz.Height, nSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); Desktop_Sharing_Shared.Screen.PInvoke.BITMAPINFO bi = new PInvoke.BITMAPINFO(); bi.bmiHeader.biSize = 40; bi.bmiHeader.biWidth = sz.Width; bi.bmiHeader.biHeight = -sz.Height; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biCompression = Desktop_Sharing_Shared.Screen.PInvoke.BitmapCompressionMode.BI_RGB; bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biXPelsPerMeter = 0; bi.bmiHeader.biYPelsPerMeter = 0; bi.bmiHeader.biClrUsed = 0; bi.bmiHeader.biClrImportant = 0; var dwBmpSize = ((sz.Width * bi.bmiHeader.biBitCount + 31) / 32) * 4 * sz.Height; raw.Data = new byte[dwBmpSize]; raw.Dimensions = new Rectangle(0, 0, sz.Width, sz.Height); Desktop_Sharing_Shared.Screen.PInvoke.GetDIBits(nSrce, nBmp, 0, Convert.ToUInt32(sz.Height), raw.Data, ref bi, PInvoke.DIB_Color_Mode.DIB_RGB_COLORS); //unsafe //{ // fixed(byte* datb = raw.Data) // { // using(Bitmap image = new Bitmap(sz.Width, sz.Height, sz.Width * 4, // PixelFormat.Format32bppRgb, new IntPtr(datb))) // { // image.Save(@"C:\Users\scott\Desktop\text.png", ImageFormat.Png); // } // } //} //Bitmap bmp = Bitmap.FromHbitmap(nBmp); PInvoke.SelectObject(nDest, hOldBmp); // return bmp; } catch(Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } // return new Bitmap(sz.Width, sz.Height); return raw; }