/// <summary> /// 加载大尺寸的头像方法,需要放到线程池中操作 /// </summary> /// <param name="largeLoadAction"></param> public void LoadUserLargePortraint(Action <string> largeLoadAction, Action largeUnloadAction) { // 先获取服务器端的MD5码 string fileMd5 = UserClient.UserAccount.LargePortraitMD5; if (string.IsNullOrEmpty(fileMd5)) { // 服务器端没有文件,加载结束 return; } // 获取本地MD5 string fileName = FileSavePath + @"\" + PortraitSupport.LargePortrait; if (System.IO.File.Exists(fileName)) { // 本地存在文件,先进行加载,如果运算不一致,再重新加载 largeLoadAction?.Invoke(fileName); string currentMd5 = null; try { currentMd5 = SoftBasic.CalculateFileMD5(fileName); } catch (Exception ex) { MessageBox.Show("计算文件MD5码错误:" + ex.Message); return; } // 对比验证 if (fileMd5 == currentMd5) { return; } } largeUnloadAction?.Invoke(); // 本地不存在文件或校验失败,需要重新下载 OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.LargePortrait, "Files", "Portrait", UserClient.UserAccount.UserName, null, fileName); if (result.IsSuccess) { // 下载成功 largeLoadAction?.Invoke(fileName); } else { MessageBox.Show("头像从服务器下载失败,错误原因:" + result.Message); } }
private void button4_Click(object sender, EventArgs e) { try { DateTime dateTime = DateTime.Now; textBox2.Text = SoftBasic.CalculateFileMD5(textBox1.Text); textBox2.AppendText(Environment.NewLine + "Total Coust:" + (DateTime.Now - dateTime).TotalSeconds.ToString("F2") + " s"); } catch (Exception ex) { MessageBox.Show("Failed:" + ex.Message); } }
public void CalculateFileMD5Example( ) { #region CalculateFileMD5Example try { string md5 = SoftBasic.CalculateFileMD5("D:\\123.txt"); Console.WriteLine(md5); } catch (Exception ex) { Console.WriteLine("failed : " + ex.Message); } #endregion }
/// <summary> /// 下载大尺寸的头像并打开它,该方法适合线程池 /// </summary> public void ThreadPoolDownloadSizeLarge() { string path = FileSavePath; string path300 = path + @"\" + PortraitSupport.LargePortrait; if (string.IsNullOrEmpty(UserClient.UserAccount.LargePortraitMD5)) { // 服务器不存在头像 return; } if (System.IO.File.Exists(path300)) { // 验证文件MD5码是否一致 string currentMd5 = SoftBasic.CalculateFileMD5(path300); if (UserClient.UserAccount.LargePortraitMD5 == currentMd5) { System.Diagnostics.Process.Start(path300); // 避免屏幕闪烁 Thread.Sleep(1000); return; } } // 验证不一致时需要利用客户端文件引擎去下载文件 OperateResult operateResult = UserClient.Net_File_Client.DownloadFile( PortraitSupport.LargePortrait, "Files", "Portrait", UserClient.UserAccount.UserName, null, path300 ); if (operateResult.IsSuccess) { System.Diagnostics.Process.Start(path300); } // 避免屏幕闪烁 Thread.Sleep(1000); }
public void DownloadUserPortraint() { string path = FileSavePath; //获取服务器文件名称 OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode.请求小头, UserClient.UserAccount.UserName); if (result.IsSuccess) { if (result.Content[0] == 'Y') { //服务器存在头像 string fileName = path + @"\" + PortraitSupport.SmallPortrait; string FileMd5 = result.Content.Substring(1); if (System.IO.File.Exists(fileName)) { //文件文件 string currentMd5 = SoftBasic.CalculateFileMD5(fileName); if (currentMd5 == FileMd5) { //加载本地头像 LoadPic?.Invoke(fileName); } else { //头像已经换了 DownloadUserPortraint(path); } } else { //客户端不存在头像 DownloadUserPortraint(path); } } else { //服务器不存在头像,本次加载结束 } } }
/// <summary> /// 点击更改头像后的操作,打开头像选择对话框,获取到2种分辨率的头像,然后进行上传 /// </summary> /// <param name="loadPic">加载大头像的委托</param> /// <param name="unloadPic">卸载大头像的委托</param> public void ChangePortrait(Action <string> loadPic, Action unloadPic) { using (FormPortraitSelect fps = new FormPortraitSelect()) { if (fps.ShowDialog() == DialogResult.OK) { string path300 = FileSavePath + @"\" + PortraitSupport.LargePortrait; string path32 = FileSavePath + @"\" + PortraitSupport.SmallPortrait; // 先卸载图片 unloadPic?.Invoke(); Bitmap bitmap300 = fps.GetSpecifiedSizeImage(300); Bitmap bitmap32 = fps.GetSpecifiedSizeImage(32); try { bitmap300.Save(path300); bitmap32.Save(path32); bitmap32.Dispose(); bitmap300.Dispose(); } catch (Exception ex) { // 文件被占用的时候无法进行覆盖 UserClient.LogNet?.WriteException("头像保存失败!", ex); MessageBox.Show("头像保存失败,原因:" + ex.Message); // 加载回旧的文件 loadPic?.Invoke(path300); return; } // 传送服务器 using (FormFileOperate ffo = new FormFileOperate( UserClient.Net_File_Client, new string[] { path300, path32 }, "Files", "Portrait", UserClient.UserAccount.UserName)) { ffo.ShowDialog(); } ThreadPool.QueueUserWorkItem(new WaitCallback(obj => { // 上传文件MD5码 string SmallPortraitMD5 = ""; string LargePortraitMD5 = ""; try { SmallPortraitMD5 = SoftBasic.CalculateFileMD5(path32); LargePortraitMD5 = SoftBasic.CalculateFileMD5(path300); } catch (Exception ex) { UserClient.LogNet.WriteException("获取文件MD5码失败:", ex); MessageBox.Show("文件信息确认失败,请重新上传!"); return; } JObject json = new JObject { { UserAccount.UserNameText, new JValue(UserClient.UserAccount.UserName) }, { UserAccount.SmallPortraitText, new JValue(SmallPortraitMD5) }, { UserAccount.LargePortraitText, new JValue(LargePortraitMD5) } }; OperateResultString result = UserClient.Net_simplify_client.ReadFromServer( CommonHeadCode.SimplifyHeadCode.头像MD5, json.ToString()); if (result.IsSuccess) { if (result.Content.Substring(0, 2) == "成功") { UserClient.UserAccount.SmallPortraitMD5 = SmallPortraitMD5; UserClient.UserAccount.LargePortraitMD5 = LargePortraitMD5; } else { MessageBox.Show("上传头像失败!原因:" + result.Content); } } else { MessageBox.Show("上传头像失败!原因:" + result.Message); } // 先显示信息 loadPic?.Invoke(path300); LoadUserSmallPortraint(); }), null); } } }
private void ThreadPoolLoadLargePortrait(object obj) { // 先获取服务器端的MD5码 string fileServerMd5 = UserClient.UserAccount.LargePortraitMD5; if (string.IsNullOrEmpty(fileServerMd5)) { return; // 没有文件 } string fileName = Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName + @"\" + PortraitSupport.LargePortrait; if (File.Exists(fileName)) { bool loadSuccess = false; Invoke(new Action(() => { try { pictureBox_UserPortrait.Image = new Bitmap(new MemoryStream(File.ReadAllBytes(fileName))); loadSuccess = true; } catch { } })); if (!loadSuccess) { goto P1; // 加载不成功,直接重新下载 } // 计算md5 string md5 = string.Empty; try { md5 = SoftBasic.CalculateFileMD5(fileName); } catch { } if (md5 == UserClient.UserAccount.LargePortraitMD5) { return; } } P1: MemoryStream ms = new MemoryStream(); OperateResult result = UserClient.Net_File_Client.DownloadFile( PortraitSupport.LargePortrait, "Files", "Portrait", UserClient.UserAccount.UserName, null, ms ); if (result.IsSuccess) { if (IsHandleCreated) { Invoke(new Action(() => { // 下载完成 Bitmap bitmap = new Bitmap(ms); pictureBox_UserPortrait.Image = bitmap; try { bitmap.Save(fileName); } catch { } })); } } else { // 下载异常,丢弃 } ms.Dispose(); }
private void pictureBox_UserPortrait_Click(object sender, EventArgs e) { // UserPortrait.ChangePortrait(LoadLargeProtrait,UnloadLargeProtrait); using (FormPortraitSelect fps = new FormPortraitSelect()) { if (fps.ShowDialog() == DialogResult.OK) { string FileSavePath = Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName; string path300 = FileSavePath + @"\" + PortraitSupport.LargePortrait; string path32 = FileSavePath + @"\" + PortraitSupport.SmallPortrait; Bitmap bitmap300 = fps.GetSpecifiedSizeImage(300); Bitmap bitmap32 = fps.GetSpecifiedSizeImage(32); try { bitmap300.Save(path300, System.Drawing.Imaging.ImageFormat.Png); bitmap32.Save(path32, System.Drawing.Imaging.ImageFormat.Png); bitmap32.Dispose(); bitmap300.Dispose(); } catch (Exception ex) { // 文件被占用的时候无法进行覆盖 UserClient.LogNet?.WriteException("头像保存失败!", ex); MessageBox.Show("头像保存失败,原因:" + ex.Message); // 加载回旧的文件 pictureBox_UserPortrait.Load(path300); return; } // 传送服务器 using (FormFileOperate ffo = new FormFileOperate( UserClient.Net_File_Client, new string[] { path300, path32 }, "Files", "Portrait", UserClient.UserAccount.UserName)) { ffo.ShowDialog(); } ThreadPool.QueueUserWorkItem(new WaitCallback(obj => { // 上传文件MD5码 string SmallPortraitMD5 = ""; string LargePortraitMD5 = ""; try { SmallPortraitMD5 = SoftBasic.CalculateFileMD5(path32); LargePortraitMD5 = SoftBasic.CalculateFileMD5(path300); } catch (Exception ex) { UserClient.LogNet.WriteException("获取文件MD5码失败:", ex); MessageBox.Show("文件信息确认失败,请重新上传!"); return; } JObject json = new JObject { { UserAccount.UserNameText, new JValue(UserClient.UserAccount.UserName) }, { UserAccount.SmallPortraitText, new JValue(SmallPortraitMD5) }, { UserAccount.LargePortraitText, new JValue(LargePortraitMD5) } }; OperateResultString result = UserClient.Net_simplify_client.ReadFromServer( CommonHeadCode.SimplifyHeadCode.头像MD5, json.ToString()); if (result.IsSuccess) { if (result.Content.Substring(0, 2) == "成功") { UserClient.UserAccount.SmallPortraitMD5 = SmallPortraitMD5; UserClient.UserAccount.LargePortraitMD5 = LargePortraitMD5; } else { MessageBox.Show("上传头像失败!原因:" + result.Content); } } else { MessageBox.Show("上传头像失败!原因:" + result.Message); } // 先显示信息 try { pictureBox_UserPortrait.Image = new Bitmap(new System.IO.MemoryStream(File.ReadAllBytes(path300))); } catch { } }), null ); } } }