private long SendImageCore(LanUser user, Image image, string imagePath) { string path; if (image != null) { path = LanConfig.Instance.GetTempFileName(".png"); image.Save(path, ImageFormat.Png); } else { path = imagePath; } long len = LanFile.GetFileLength(path); if (len > UdpClientEx.UDP_MAX_BUF_SIZE) { //图像文件过大的话用文件形式发送 return(SendFile(user, path, true)); } else { UdpPacket packet = new UdpPacket(); packet.Address = user.IP; packet.Port = user.Port; packet.ToMAC = user.MAC; packet.Command = UdpPacket.CMD_SEND_IMAGE | UdpPacket.CMD_OPTION_NEED_RESPONSE; packet.FromMAC = this.MAC; UdpPacketImageExtend extend = new UdpPacketImageExtend(); extend.EncryptKey = user.SecurityKeys.Public; extend.Image = image ?? Image.FromFile(path); packet.Extend = extend; _client.Send(packet); if (image != null) { //直接发送图像的话,临时图片就删除 LanFile.Delete(path); } return(packet.ID); } }
private void SendFile_Click(object sender, EventArgs e) { if (!this.SendMessageEnabled) { return; } using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Filter = "所有文件|*.*"; if (ofd.ShowDialog(this) == DialogResult.OK) { string fileName = ofd.FileName; long id = User.SendFile(Contacter, fileName); //保存发送记录 Store.Models.FileMessage m = new Store.Models.FileMessage(); m.FromUserId = this.User.ID; m.ToUserId = this.Contacter.ID; m.OriginFilePath = fileName; m.FileName = Path.GetFileName(fileName); m.FileLength = LanFile.GetFileLength(fileName); m.Flag = true; //默认成功,后面按照失败结果设定为false MessageListItem item = new MessageListItem(); item.ID = id; item.Message = m; item.User = this.User; item.State = MessageState.Sending; item.Save(); AddMessageItem(item, true); OnSendMessage(m); } } }