private void Form2_FormClosing(object sender, FormClosingEventArgs e) //点击窗口x调用该函数 { if (netflag == 0) //判断网络标志是否为0,也就是是否成功连接到网络。 { newtcpclient.Send(StaticTools.CombomBinaryArray(BitConverter.GetBytes(firstflag), BitConverter.GetBytes(nextflag), Encoding.UTF8.GetBytes("exitthis"))); //设置字符编码为UTF-8并发送退出消息 newtcpclient.Shutdown(SocketShutdown.Both); //停止socket连接 newtcpclient.Close(); //关闭socket连接 } thread.Abort(); //关闭子线程 f1.Show(); //显示登录窗口form1 }
public void button1_Click(object sender, EventArgs e) //点击发送按钮调用该函数 { if (!richTextBox2.Text.Equals("")) //判断输入的消息是否为空 { if (netflag == 0) //判断网络连接是否成功 { byte[] tosend = Encoding.UTF8.GetBytes("\n" + name + ":\n" + richTextBox2.Text + "\n"); byte[] temp = StaticTools.CombomBinaryArray(BitConverter.GetBytes(firstflag), BitConverter.GetBytes(nextflag), tosend); newtcpclient.Send(temp); //发送socket消息,并编码UTF-8 richTextBox2.Text = ""; //清空发送栏 pictobyte.Clear(); //清空表情转字节流的缓存区 richTextBox2.Focus(); //将光标聚焦在输入框 } } }
private void button4_Click(object sender, EventArgs e) { //初始化一个OpenFileDialog类 OpenFileDialog fileDialog = new OpenFileDialog(); //fileDialog.Filter = "(*.png)|*.png|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg"; //判断用户是否正确的选择了文件 if (fileDialog.ShowDialog() == DialogResult.OK) { //获取用户选择文件的后缀名 string extension = Path.GetExtension(fileDialog.FileName); //声明允许的后缀名 //获取用户选择的文件,并判断文件大小不能超过2000K,fileInfo.Length是以字节为单位的 FileInfo fileInfo = new FileInfo(fileDialog.FileName); if (fileInfo.Length > 2048000) { MessageBox.Show("上传的文件不能大于2000K"); } else //在这里就可以写获取到正确文件后的代码了 { string filepath = Path.GetFullPath(fileDialog.FileName); byte[] bytes = StaticTools.GetFileBytes(filepath); //图片路径,转成字节流 MessageBox.Show("创建字节流成功" + filepath); byte[] fileext = new byte[4]; fileext = Encoding.UTF8.GetBytes(Path.GetExtension(filepath)); //client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//实例化Socket对象 //client.Connect(iep);//与该ip地址进行连接 byte[] datasize = new byte[4]; datasize = BitConverter.GetBytes(bytes.Length); //把长度作为16进制数放在datasize中 byte[] temp = new byte[6]; temp = StaticTools.CombomBinaryArray(BitConverter.GetBytes(true), BitConverter.GetBytes(false), datasize, fileext); newtcpclient.Send(temp); //发送字节流长度给服务器 newtcpclient.Send(bytes, bytes.Length, SocketFlags.None); //发送图片字节 } //} } }
private void pictureBox_Click(object sender, EventArgs e) { PictureBox pic = sender as PictureBox;//定义鼠标当前点击picureBox的行为 if (pic == null) { return; } string emojinumber = ((sender) as PictureBox).Tag.ToString(); Image clickemoji = pic.Image; MessageBox.Show(string.Format("选中{0}号表情", emojinumber)); //pictobyte.Add(StaticTools.BmpConvertByte(clickemoji)); //Clipboard.SetDataObject(clickemoji); //richTextBox2.Paste(DataFormats.GetFormat(DataFormats.Bitmap)); //StaticTools.InsertImage(this.richTextBox1,clickemoji); //Clipboard.Clear(); //byte[] tempemoji = new byte[2048]; //byte[] tempemoji=StaticTools.BmpConvertByte(clickemoji); //int tempsize = tempemoji.Length; byte[] size = new byte[4]; byte[] selectidx = new byte[4]; bool createidxofemoji; if (!(createidxofemoji = StaticTools.ConvertIntToByteArray(int.Parse(emojinumber), ref selectidx))) { return; } nextflag = true; newtcpclient.Send(StaticTools.CombomBinaryArray(BitConverter.GetBytes(firstflag), BitConverter.GetBytes(nextflag), selectidx)); //newtcpclient.Send(tempemoji); //richTextBox2.AppendText(clickemoji.ToString()); ////string tag = pic.Tag.ToString(); //string tag = pic.Created.ToString(); //MessageBox.Show(tag);//显示每一个图片位置编号,其他的功能根据自己需要扩展 ////this.groupBox.Refresh(); }