/// <summary> /// Determine type of data received and retrieve the information sent for display. /// </summary> private void InterpetData(string data) { // Check what type of data we have dynamic type = GetDataType(data); if (type == typeof(int)) { // Retrieve buffer length int length = int.Parse(data, NumberFormatInfo.InvariantInfo); // Retrieve bytes from data stream byte[] bytes = binaryReader.ReadBytes(length); // Convert bytes to image and display it PrintImage(CchatImage.GetImageFromByteArray(bytes)); //pictureBox.Image.Save(Path + "\\Images", ImageFormat.Png); } else if (type == typeof(string)) { if (data.Contains("sender:")) { PrintText(data); //CchatLog.WriteToLog(MSG_PREFIX_RECEIVER + data); } else if (data == "/disconnect") { Disconnect(); return; } } }
private void dataSender_DoWork(object sender, DoWorkEventArgs e) { if (client != null && client.Connected) { if (image_to_send != null) { // Convert Image to bytes byte[] imageBytes = CchatImage.GetByteArrayFromImage(image_to_send); // Store length of the byte array of the Image string imageLength = "" + imageBytes.Length; // Send the length streamWriter.WriteLine(imageLength); // Send the Image binaryWriter.Write(imageBytes, 0, imageBytes.Length); // Show Image to self PrintImage(image_to_send); // Clean up old image to send image_to_send = null; // Wait before sending next Thread.Sleep(1000); } if (text_to_send != null) { // Send text written in the textbox streamWriter.WriteLine(MSG_PREFIX_RECEIVER + text_to_send); // Show text written to self PrintText(MSG_PREFIX_SENDER + text_to_send); // Store text written in textbox to log CchatLog.WriteToLog(MSG_PREFIX_SENDER + text_to_send); // Clean up old text to send text_to_send = null; } } else { PrintText(MSG_ERROR_SEND_DATA); return; } }