Esempio n. 1
0
        private void sendMessage(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textboxMessage.Text))
            {
                MyBubble bubble = new MyBubble(1);
                bubble.Dock = DockStyle.Bottom;
                bubble.SendToBack();
                bubble.Body = textboxMessage.Text;
                currentRoom.myPanel.Controls.Add(bubble);
                int id = Program.RandomNumber(0, 2 ^ 32);

                Client.ModelObject Send = new Client.ModelObject();
                Send.command.code = "5";
                Send.command.id   = $"{id}";
                Send.message.type = "1";
                Send.message.data = textboxMessage.Text;
                Send.room.id      = currentRoom.IDROOM;


                register.socket.Send(Send);
                textboxMessage.Text = "";
            }
        }
Esempio n. 2
0
        private void btnAddFile_Click(object sender, EventArgs e)
        {
            var fileContent = string.Empty;
            var filePath    = string.Empty;

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter           = "Image Files(*.jpg, *png)|*jpg;*png|Txt files(*.txt)|*.txt|Pdf files(*.pdf)|*pdf|All files (*.*)|*.*";
                openFileDialog.FilterIndex      = 4;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fi       = new FileInfo(openFileDialog.FileName);
                    long     fileSize = fi.Length;
                    if (fileSize > 10 * 1024 * 1024)
                    {
                        MessageBox.Show("The size is over limited size.");
                        return;
                    }
                    filePath = openFileDialog.FileName;
                    string[] partsFileName = filePath.Split('.');

                    if (partsFileName[partsFileName.Length - 1] == "jpg" || partsFileName[partsFileName.Length - 1] == "png")
                    {
                        byte[] bytesFromImage = ImageToByteArray(Image.FromFile(filePath));
                        string base64OfImgae  = Convert.ToBase64String(bytesFromImage);
                        Image  x  = Base64ToImage(base64OfImgae);
                        Upload ul = new Upload(x);


                        Form form1 = new Form();
                        form1.BackColor       = System.Drawing.Color.Black;
                        form1.Opacity         = (double)0.5;
                        form1.FormBorderStyle = FormBorderStyle.None;
                        form1.Height          = ActiveForm.Height;
                        form1.Width           = ActiveForm.Width;
                        form1.FormBorderStyle = FormBorderStyle.None;


                        form1.ShowIcon      = false;
                        form1.ShowInTaskbar = false;

                        form1.BringToFront();
                        form1.Show();

                        ul.BringToFront();
                        ul.ShowDialog();
                        form1.Dispose();

                        if (ul.flag)
                        {
                            MyBubble bubble = new MyBubble(0);
                            bubble.Dock = DockStyle.Bottom;
                            bubble.SendToBack();
                            bubble.ImageOfMessage = x;
                            currentRoom.myPanel.Controls.Add(bubble);
                            int id = Program.RandomNumber(0, 2 ^ 32);

                            Client.ModelObject Send = new Client.ModelObject();
                            Send.command.code = "5";
                            Send.command.id   = $"{id}";
                            Send.message.type = "0";
                            Send.room.id      = currentRoom.IDROOM;
                            Send.message.data = base64OfImgae;
                            register.socket.Send(Send);
                        }
                    }
                    else
                    {
                        byte[] bytesFromFile = File.ReadAllBytes(filePath);
                        string base64OfFile  = Convert.ToBase64String(bytesFromFile);

                        MyBubble bubble = new MyBubble(2);
                        bubble.Dock = DockStyle.Bottom;
                        bubble.SendToBack();

                        bubble.ChatTextCursor = Cursors.Hand;
                        bubble.Body           = $"Đã gửi {filePath.Split('\\')[filePath.Split('\\').Length - 1]}";

                        currentRoom.myPanel.Controls.Add(bubble);

                        string             body = $"{bubble.Body}[;]{base64OfFile}";
                        int                id   = Program.RandomNumber(0, 2 ^ 32);
                        Client.ModelObject Send = new Client.ModelObject();
                        Send.command.code = "5";
                        Send.command.id   = $"{id}";
                        Send.message.type = "2";
                        Send.room.id      = currentRoom.IDROOM;
                        Send.message.data = body;
                        register.socket.Send(Send);
                    }
                }
            }
        }