Exemple #1
0
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //Opens a filedialog where a picture is chosen. (only works for smaller sizes, increase the receive buffer size to send larger files
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; )|*.jpg;";

                if (open.ShowDialog() == true)
                {
                    //Specifices the image source
                    string ImgSrc  = open.FileName;
                    byte[] imgBuff = File.ReadAllBytes(ImgSrc);

                    DataPacket DP = new DataPacket(4, MyUsername, "Image", imgBuff);
                    Db.AddToChatHistory(DP, MyUsername);
                    SocketClass.Send(DP);

                    //Converts the imgbuffer to a picture that can be displayed in the chat
                    using (var ms = new System.IO.MemoryStream(imgBuff))
                    {
                        var image = new BitmapImage();
                        image.BeginInit();
                        image.CacheOption       = BitmapCacheOption.OnLoad; // here
                        image.StreamSource      = ms;
                        image.DecodePixelHeight = 100;
                        image.DecodePixelWidth  = 100;
                        image.EndInit();
                        image.Freeze();

                        Eventaction_DisplayImage(image, new EventArgs());
                    }
                    ConvoBox.Items.Add(DateTime.Now + " Me: " + "Image");
                    MessageLine.Clear();
                }
            }
            catch (SocketException ex)
            {
                MessageBox.Show("A socket error occurred: " + ex.ToString());
                Console.WriteLine(ex.StackTrace);
            }
            catch (Exception ex)
            {
                MessageBox.Show("A error occured: " + ex.ToString());
                Console.WriteLine(ex.StackTrace);
            }
        }
Exemple #2
0
 private void SendButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         //Creates a packet with the specific messages and sends it.
         DataPacket DP = new DataPacket(0, MyUsername, MessageLine.Text, new byte[1]);
         Db.AddToChatHistory(DP, MyUsername);
         SocketClass.Send(DP);
         ConvoBox.Items.Add(DateTime.Now + " Me: " + MessageLine.Text);
         MessageLine.Clear();
     }
     catch (SocketException ex)
     {
         MessageBox.Show("A socket error occurred: " + ex.ToString());
         Console.WriteLine(ex.StackTrace);
     }
     catch (Exception ex)
     {
         MessageBox.Show("A error occured: " + ex.ToString());
         Console.WriteLine(ex.StackTrace);
     }
 }