public void run() { NetworkStream serverStream = null; QuickLZ lz = new QuickLZ(); try { clientSocket.Connect(ScreenCasterSystemTray.Program.hostName, 6000); serverStream = clientSocket.GetStream(); } catch (Exception e) { useSocket = false; MessageBox.Show("Could not connect to '" + ScreenCasterSystemTray.Program.hostName + "' port 6000"); return; } DxScreenCapture sc = new DxScreenCapture(); UInt32[] screenbuf = new UInt32[1920 * 1200]; byte[] outputBuf = new byte[1024 * 10000]; byte[] outputBufCompressed = new byte[1024 * 10000]; bool done = false; try { while (!done) { x1 = -1; x2 = -1; y1 = -1; y2 = -1; Surface s = sc.CaptureScreen(); System.Drawing.Point mPosition = Cursor.Position; //s.LockRectangle(LockFlags.ReadOnly); DataRectangle dr = s.LockRectangle(LockFlags.None); DataStream gs = dr.Data; for (int y = 0; y < screenHeight; y++) { for (int x = 0; x < screenWidth; x++) { UInt32 pixel = gs.Read<UInt32>(); int offset = ((y * screenWidth) + x); if (pixel != screenbuf[offset]) { differentPixel(x, y); } screenbuf[offset] = pixel; } } gs.Dispose(); while ((x1 % 4) != 0) x1--; while ((x2 % 4) != 0) x2++; while ((y1 % 4) != 0) y1--; while ((y2 % 4) != 0) y2++; s.UnlockRectangle(); if (x1 == -4) { x1 = 0; x2 = 4; y1 = 0; y2 = 4; } if (x1 != -4) { DataStream ds = Surface.ToStream(s, ImageFileFormat.Bmp, new System.Drawing.Rectangle(x1, y1, x2 - x1, y2 - y1)); int dsLength = Convert.ToInt32(ds.Length); ds.Read(outputBuf, 0, dsLength); ds.Dispose(); uint compressedLength = lz.Compress(outputBuf, outputBufCompressed, dsLength); int mouseX = mPosition.X; int mouseY = mPosition.Y; if (useSocket) { byte[] header = new byte[24]; header[0] = 1; header[1] = 2; header[2] = 3; header[3] = 4; int length = Convert.ToInt32(compressedLength); intToBytes(header, 4, length); intToBytes(header, 8, x1); intToBytes(header, 12, y1); intToBytes(header, 16, mouseX); intToBytes(header, 20, mouseY); serverStream.Write(header, 0, header.Length); serverStream.Write(outputBufCompressed, 0, (int)compressedLength); serverStream.Flush(); } } s.Dispose(); Thread.Sleep(100); } } catch (Exception e) { MessageBox.Show("Connection lost in screengrab"); return; } }
public void run() { NetworkStream serverStream = null; QuickLZ lz = new QuickLZ(); try { clientSocket.Connect(ScreenCasterSystemTray.Program.hostName, 6000); serverStream = clientSocket.GetStream(); } catch (Exception e) { useSocket = false; MessageBox.Show("Could not connect to '" + ScreenCasterSystemTray.Program.hostName + "' port 6000"); return; } DxScreenCapture sc = new DxScreenCapture(); UInt32[] screenbuf = new UInt32[1920 * 1200]; byte[] outputBuf = new byte[1024 * 10000]; byte[] outputBufCompressed = new byte[1024 * 10000]; bool done = false; try { while (!done) { x1 = -1; x2 = -1; y1 = -1; y2 = -1; Surface s = sc.CaptureScreen(); System.Drawing.Point mPosition = Cursor.Position; //s.LockRectangle(LockFlags.ReadOnly); DataRectangle dr = s.LockRectangle(LockFlags.None); DataStream gs = dr.Data; for (int y = 0; y < screenHeight; y++) { for (int x = 0; x < screenWidth; x++) { UInt32 pixel = gs.Read <UInt32>(); int offset = ((y * screenWidth) + x); if (pixel != screenbuf[offset]) { differentPixel(x, y); } screenbuf[offset] = pixel; } } gs.Dispose(); while ((x1 % 4) != 0) { x1--; } while ((x2 % 4) != 0) { x2++; } while ((y1 % 4) != 0) { y1--; } while ((y2 % 4) != 0) { y2++; } s.UnlockRectangle(); if (x1 == -4) { x1 = 0; x2 = 4; y1 = 0; y2 = 4; } if (x1 != -4) { DataStream ds = Surface.ToStream(s, ImageFileFormat.Bmp, new System.Drawing.Rectangle(x1, y1, x2 - x1, y2 - y1)); int dsLength = Convert.ToInt32(ds.Length); ds.Read(outputBuf, 0, dsLength); ds.Dispose(); uint compressedLength = lz.Compress(outputBuf, outputBufCompressed, dsLength); int mouseX = mPosition.X; int mouseY = mPosition.Y; if (useSocket) { byte[] header = new byte[24]; header[0] = 1; header[1] = 2; header[2] = 3; header[3] = 4; int length = Convert.ToInt32(compressedLength); intToBytes(header, 4, length); intToBytes(header, 8, x1); intToBytes(header, 12, y1); intToBytes(header, 16, mouseX); intToBytes(header, 20, mouseY); serverStream.Write(header, 0, header.Length); serverStream.Write(outputBufCompressed, 0, (int)compressedLength); serverStream.Flush(); } } s.Dispose(); Thread.Sleep(100); } } catch (Exception e) { MessageBox.Show("Connection lost in screengrab"); return; } }