private void Timer_Elapsed(object sender, EventArgs e) { if (networkStream.DataAvailable) { int length = (int)formatter.Deserialize(networkStream); byte[] buffer = new byte[length]; int position = 0; while (position < length) { position += networkStream.Read(buffer, position, length - position); } //networkStream.Read(buffer, 0, (int)length); MemoryStream mem = new MemoryStream(buffer); mem.Position = 0; MemoryStream decompressed = new MemoryStream(); ShortEncoder.Decode(mem, decompressed); decompressed.Position = 0; screenImg = encoder.LoadFromStr(decompressed); //screenImg = new Bitmap(mem); NewFrame.Invoke(screenImg); } else { ClientCommand clientCommand = new ClientCommand { needFrame = true, needHeight = height, needWidth = width }; formatter.Serialize(networkStream, JsonConvert.SerializeObject(clientCommand)); } }
void SendFrame(ClientCommand command, Bitmap screenImg) { if (encoder == null) { encoder = new MyEncoder(command.needWidth, command.needHeight); } Bitmap small_map = new Bitmap(screenImg, command.needWidth, command.needHeight); MemoryStream str = new MemoryStream(); encoder.WriteToStr(small_map, str); MemoryStream compressed = new MemoryStream(); str.Position = 0; ShortEncoder.Encode(str, compressed); compressed.Position = 0; lock (tcpLock) { formatter.Serialize(networkStream, (int)compressed.Length); compressed.CopyTo(networkStream); } }