private static void CopyToImage(Bitmap image, ScreenFrame screenFrame) { var imageBoundaries = new Rectangle(0, 0, screenFrame.Boundaries.Right, screenFrame.Boundaries.Bottom); var imageData = image.LockBits(imageBoundaries, ImageLockMode.WriteOnly, image.PixelFormat); Marshal.Copy(screenFrame.NewPixels, 0, imageData.Scan0, screenFrame.NewPixels.Length); image.UnlockBits(imageData); }
private static void ProcessModifiedRegions(BinaryWriter binaryWriter, ScreenFrame frame) { var pi = 0; // Iterate through each modified region. foreach (var modifiedRegion in frame.ModifiedRegions) { var n = frame.Boundaries.Right * 4; // Iterate from top to bottom. for (int y = modifiedRegion.Top, yi = y * n; y < modifiedRegion.Bottom; y++, yi += n) { // Iterate from left to right. for (int x = modifiedRegion.Left, ci = yi + x * 4; x < modifiedRegion.Right; x++, ci += 4) { // Check if the pixel has not been changed. if (frame.NewPixels[ci + 0] == frame.PreviousPixels[ci + 0] && frame.NewPixels[ci + 1] == frame.PreviousPixels[ci + 1] && frame.NewPixels[ci + 2] == frame.PreviousPixels[ci + 2] && frame.NewPixels[ci + 3] == frame.PreviousPixels[ci + 3]) continue; // Write the index mutation, each channel, and track the index. binaryWriter.WriteSignedVariableLength(ci - pi); binaryWriter.Write((byte) ((frame.NewPixels[ci + 0] & 248) | (frame.NewPixels[ci + 1] & 224) >> 5)); binaryWriter.Write((byte) ((frame.NewPixels[ci + 2] & 248) | (frame.NewPixels[ci + 1] & 28) >> 2)); pi = ci; } } } }
private void ProcessMovedRegions(ScreenFrame frame) { _writer.WriteUnsignedVariableLength((uint)frame.MovedRegions.Length); foreach (var region in frame.MovedRegions) { _writer.WriteUnsignedVariableLength((uint)region.X); _writer.WriteUnsignedVariableLength((uint)region.Y); _writer.WriteUnsignedVariableLength((uint)region.Destination.Left); _writer.WriteUnsignedVariableLength((uint)region.Destination.Top); _writer.WriteUnsignedVariableLength((uint)(region.Destination.Right - region.Destination.Left)); _writer.WriteUnsignedVariableLength((uint)(region.Destination.Bottom - region.Destination.Top)); } }
private static void ProcessMovedRegions(BinaryWriter binaryWriter, ScreenFrame frame) { // Write the number of moved regions. binaryWriter.WriteUnsignedVariableLength((uint)frame.MovedRegions.Length); // Iterate through each moved region. foreach (var movedRegion in frame.MovedRegions) { binaryWriter.WriteUnsignedVariableLength((uint) movedRegion.X); binaryWriter.WriteUnsignedVariableLength((uint) movedRegion.Y); binaryWriter.WriteUnsignedVariableLength((uint) movedRegion.Destination.Left); binaryWriter.WriteUnsignedVariableLength((uint) movedRegion.Destination.Top); binaryWriter.WriteUnsignedVariableLength((uint) (movedRegion.Destination.Right - movedRegion.Destination.Left)); binaryWriter.WriteUnsignedVariableLength((uint) (movedRegion.Destination.Bottom - movedRegion.Destination.Top)); } }
public async Task StartAsync(ScreenFrame frame) { _buffer = new byte[frame.Boundaries.Bottom * frame.Boundaries.Right * 7]; _bufferCompressed = new byte[frame.Boundaries.Bottom * frame.Boundaries.Right * 7]; _writer = new QuickBinaryWriter(_buffer); _writer.WriteUnsignedVariableLength((uint)frame.Boundaries.Right); _writer.WriteUnsignedVariableLength((uint)frame.Boundaries.Bottom); for (var i = 0; i < frame.NewPixels.Length; i += 4) { _writer.WriteByte((byte)((frame.NewPixels[i + 0] & 248) | (frame.NewPixels[i + 1] & 224) >> 5)); _writer.WriteByte((byte)((frame.NewPixels[i + 2] & 248) | (frame.NewPixels[i + 1] & 28) >> 2)); } var len = _buffer.CompressQuick(0, _writer.Position, _bufferCompressed); await _protocolStream.SendAsync(_bufferCompressed, 0, len); // await _protocolStream.SendAsync(_buffer, 0, _writer.Position); }
public async Task StartAsync(ScreenFrame frame) { _buffer = new byte[frame.Boundaries.Bottom * frame.Boundaries.Right * 4]; using (var memoryStream = new MemoryStream(_buffer)) { using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.Default, true)) { binaryWriter.Write((ushort)frame.Boundaries.Right); binaryWriter.Write((ushort)frame.Boundaries.Bottom); for (var i = 0; i < frame.NewPixels.Length; i += 4) { binaryWriter.Write((byte) ((frame.NewPixels[i + 0] & 248) | (frame.NewPixels[i + 1] & 224) >> 5)); binaryWriter.Write((byte) ((frame.NewPixels[i + 2] & 248) | (frame.NewPixels[i + 1] & 28) >> 2)); } } await _protocolStream.SendAsync(_buffer, 0, (int) memoryStream.Position); } }
private void ProcessModifiedRegions(ScreenFrame frame) { var n = frame.NewPixels; var p = frame.PreviousPixels; var s = 0; var w = frame.Boundaries.Right * 4; foreach (var region in frame.ModifiedRegions) { for (int y = region.Top, yOffset = y * w; y < region.Bottom; y++, yOffset += w) { for (int x = region.Left, xOffset = x * 4, i = yOffset + xOffset; x < region.Right; x++, i += 4) { if (n[i] == p[i] && n[i + 1] == p[i + 1] && n[i + 2] == p[i + 2]) continue; _writer.WriteSignedVariableLength(i - s); _writer.WriteByte((byte)((n[i + 0] & 248) | (n[i + 1] & 224) >> 5)); _writer.WriteByte((byte)((n[i + 2] & 248) | (n[i + 1] & 28) >> 2)); s = i; } } } }
public async Task UpdateAsync(ScreenFrame frame) { var beginTime = DateTime.Now; _writer.Position = 0; ProcessMovedRegions(frame); ProcessModifiedRegions(frame); var len = _buffer.CompressQuick(0, _writer.Position, _bufferCompressed); if ((DateTime.Now - beginTime).TotalMilliseconds > 30) { Console.WriteLine("Proc in {0}ms", (DateTime.Now - beginTime).TotalMilliseconds); } await _protocolStream.SendAsync(_bufferCompressed, 0, len); // await _protocolStream.SendAsync(_buffer, 0, _writer.Position); if ((DateTime.Now - beginTime).TotalMilliseconds > 30) { Console.WriteLine("Sent in {0}ms", (DateTime.Now - beginTime).TotalMilliseconds); } }
private static Bitmap CreateImage(ScreenFrame screenFrame) { return new Bitmap(screenFrame.Boundaries.Right, screenFrame.Boundaries.Bottom, PixelFormat.Format32bppRgb); }
public async Task UpdateAsync(ScreenFrame frame) { using (var memoryStream = new MemoryStream(_buffer)) { using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.Default, true)) { ProcessMovedRegions(binaryWriter, frame); ProcessModifiedRegions(binaryWriter, frame); } await _protocolStream.SendAsync(_buffer, 0, (int)memoryStream.Position); } }