diff --git a/ClientsLibrary/AccountSupport/FormAccountDetails.cs b/ClientsLibrary/AccountSupport/FormAccountDetails.cs index 8661242..f4a0a61 100644 --- a/ClientsLibrary/AccountSupport/FormAccountDetails.cs +++ b/ClientsLibrary/AccountSupport/FormAccountDetails.cs @@ -13,6 +13,7 @@ using HslCommunication; using HslCommunication.BasicFramework; using System.IO; using ClientsLibrary.FileSupport; +using Newtonsoft.Json.Linq; namespace ClientsLibrary { @@ -21,11 +22,10 @@ namespace ClientsLibrary #region Constructor - public FormAccountDetails(UserPortrait userPortrait) + public FormAccountDetails() { InitializeComponent(); Icon = UserClient.GetFormWindowIcon(); - UserPortrait = userPortrait; } #endregion @@ -107,47 +107,202 @@ namespace ClientsLibrary #region Load Portrait - public UserPortrait UserPortrait { get; } - - + + private void pictureBox_UserPortrait_Click(object sender, EventArgs e) { - UserPortrait.ChangePortrait(LoadLargeProtrait,UnloadLargeProtrait); + // 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 + ); + } + + } } private void ThreadPoolLoadLargePortrait(object obj) { - Thread.Sleep(200); - UserPortrait.LoadUserLargePortraint(LoadLargeProtrait, UnloadLargeProtrait); - } - - private void UnloadLargeProtrait() - { - if (IsHandleCreated && InvokeRequired) + // 先获取服务器端的MD5码 + string fileServerMd5 = UserClient.UserAccount.LargePortraitMD5; + if (string.IsNullOrEmpty(fileServerMd5)) { - BeginInvoke(new Action(() => - { - UnloadLargeProtrait(); - })); - return; + return; // 没有文件 } - pictureBox_UserPortrait.Image = null; - } - - private void LoadLargeProtrait(string fileName) - { - if (IsHandleCreated && InvokeRequired) + string fileName = Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName + @"\" + PortraitSupport.LargePortrait; + if(File.Exists(fileName)) { - BeginInvoke(new Action(() => + bool loadSuccess = false; + Invoke(new Action(() => { - LoadLargeProtrait(fileName); + try + { + pictureBox_UserPortrait.Image = new Bitmap(new MemoryStream(File.ReadAllBytes(fileName))); + loadSuccess = true; + } + catch + { + + } })); - return; + + 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 + { + // 下载异常,丢弃 } - pictureBox_UserPortrait.ImageLocation = fileName; + ms.Dispose(); } + #endregion diff --git a/ClientsLibrary/AccountSupport/UserPortrait.cs b/ClientsLibrary/AccountSupport/UserPortrait.cs index 147a8b6..468f6f7 100644 --- a/ClientsLibrary/AccountSupport/UserPortrait.cs +++ b/ClientsLibrary/AccountSupport/UserPortrait.cs @@ -26,7 +26,7 @@ namespace ClientsLibrary /// - /// 头像管理类 + /// 头像管理器,负责所有小头像数据的缓存 /// public class UserPortrait { @@ -37,7 +37,7 @@ namespace ClientsLibrary /// /// 头像存储的文件夹路径 /// 加载头像的委托 - public UserPortrait(string filePath, Action loadPicSmall) + public UserPortrait(string filePath) { if (!System.IO.Directory.Exists(filePath)) { @@ -45,229 +45,10 @@ namespace ClientsLibrary } FileSavePath = filePath; - m_LoadPicSmall = loadPicSmall; } #endregion - #region Change Portrait - - /// - /// 点击更改头像后的操作,打开头像选择对话框,获取到2种分辨率的头像,然后进行上传 - /// - /// 加载大头像的委托 - /// 卸载大头像的委托 - public void ChangePortrait(Action 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); - - } - - } - } - - - #endregion - - #region Load Portraint - - public static Bitmap DownloadSmallPortraint(string userName) - { - Bitmap bitmap = null; - - System.IO.MemoryStream ms = new System.IO.MemoryStream(); - - OperateResult result = UserClient.Net_File_Client.DownloadFile( - PortraitSupport.SmallPortrait, - "Files", - "Portrait", - userName, - null, - ms - ); - if (result.IsSuccess) - { - bitmap = new Bitmap(ms); - } - else - { - bitmap = Properties.Resources.person_1; - } - - ms.Dispose(); - - return bitmap; - } - - /// - /// 加载小尺寸的头像操作,需要放置到线程池中 - /// - public void LoadUserSmallPortraint() - { - m_LoadPicSmall?.Invoke(DownloadSmallPortraint(UserClient.UserAccount.UserName)); - } - - - /// - /// 加载大尺寸的头像方法,需要放到线程池中操作 - /// - /// - public void LoadUserLargePortraint(Action 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); - } - - - } - - #endregion - #region Download Portraint @@ -323,19 +104,62 @@ namespace ClientsLibrary #endregion - #region Public Method - /// - /// - /// - /// - public string GetLargePortraitFileName() + #region Pricvate Method + + private void SetSmallPortrait(string name,Bitmap bitmap) { - return FileSavePath + @"\" + PortraitSupport.LargePortrait; + hybirdLock.Enter(); + + if(dictSmallPortrait.ContainsKey(name)) + { + dictSmallPortrait[name] = bitmap; + } + else + { + dictSmallPortrait.Add(name, bitmap); + } + + hybirdLock.Leave(); } - public string GetSmallPortraitFileName() + private Bitmap DownloadSmallPortraitByName(string name) { - return FileSavePath + @"\" + PortraitSupport.SmallPortrait; + OperateResult result = UserClient.Net_File_Client.DownloadFile( + PortraitSupport.SmallPortrait, + "Files", + "Portrait", + name, + null, + out Bitmap bitmap); + if (result.IsSuccess) + { + SetSmallPortrait(name, bitmap); + return bitmap; + } + else + { + return Properties.Resources.person_1; + } + } + + public void UpdateSmallPortraitByName(string name) + { + DownloadSmallPortraitByName(name); + } + + public Bitmap GetSmallPortraitByUserName(string name) + { + Bitmap bitmap = null; + hybirdLock.Enter(); + if (dictSmallPortrait.ContainsKey(name)) + { + bitmap = dictSmallPortrait[name]; + } + hybirdLock.Leave(); + + if (bitmap != null) return bitmap; + + return DownloadSmallPortraitByName(name); } #endregion @@ -346,11 +170,9 @@ namespace ClientsLibrary /// 文件的路径 /// private string FileSavePath { get; set; } - - /// - /// 加载小头像的委托 - /// - private Action m_LoadPicSmall { get; set; } + + private Dictionary dictSmallPortrait = new Dictionary(); + private SimpleHybirdLock hybirdLock = new SimpleHybirdLock(); #endregion } diff --git a/ClientsLibrary/BasicSupport/NetClientItem.cs b/ClientsLibrary/BasicSupport/NetClientItem.cs index e3b26e8..87cab20 100644 --- a/ClientsLibrary/BasicSupport/NetClientItem.cs +++ b/ClientsLibrary/BasicSupport/NetClientItem.cs @@ -77,33 +77,14 @@ namespace ClientsLibrary.BasicSupport // 向服务器请求小头像 if (m_NetAccount != null) { - try - { - System.IO.MemoryStream ms = new System.IO.MemoryStream(); + Bitmap bitmap = UserClient.PortraitManager.GetSmallPortraitByUserName(m_NetAccount.UserName); - HslCommunication.OperateResult result = UserClient.Net_File_Client.DownloadFile( - PortraitSupport.SmallPortrait, - "Files", - "Portrait", - m_NetAccount.UserName, - null, - ms - ); - if (result.IsSuccess) - { - Bitmap bitmap = new Bitmap(ms); - pictureBox1.Image = bitmap; - } - else - { - // 可能网络错误,也可能因为服务器没有头像 - // MessageBox.Show(result.Message); - pictureBox1.Image = Properties.Resources.person_1; - } - } - catch (Exception ex) + if(IsHandleCreated) { - MessageBox.Show(ex.Message); + Invoke(new Action(() => + { + pictureBox1.Image = bitmap; + })); } } } diff --git a/ClientsLibrary/UserClient.cs b/ClientsLibrary/UserClient.cs index efe7644..0b7c2b7 100644 --- a/ClientsLibrary/UserClient.cs +++ b/ClientsLibrary/UserClient.cs @@ -36,7 +36,7 @@ namespace ClientsLibrary /// /// 本软件的当前版本,用来验证更新的关键依据 /// - public static SystemVersion CurrentVersion { get; } = new SystemVersion("1.0.0.171013"); + public static SystemVersion CurrentVersion { get; } = new SystemVersion("1.0.0.171014"); /// @@ -133,8 +133,10 @@ namespace ClientsLibrary return false; } - - + /// + /// 头像管理器 + /// + public static UserPortrait PortraitManager { get; set; } diff --git a/CommonLibrary/ProtocolSupport/CommonProtocol.cs b/CommonLibrary/ProtocolSupport/CommonProtocol.cs index 40c472c..c981d0e 100644 --- a/CommonLibrary/ProtocolSupport/CommonProtocol.cs +++ b/CommonLibrary/ProtocolSupport/CommonProtocol.cs @@ -45,7 +45,7 @@ namespace CommonLibrary * **************************************************************************/ - SoftBasic.FrameworkVersion = new SystemVersion("1.6.12"); + SoftBasic.FrameworkVersion = new SystemVersion("1.6.13"); } diff --git a/Public/HslCommunication.dll b/Public/HslCommunication.dll index 60a691d..923d5ac 100644 Binary files a/Public/HslCommunication.dll and b/Public/HslCommunication.dll differ diff --git a/Public/HslCommunication.xml b/Public/HslCommunication.xml index 69a88e7..008c2f7 100644 --- a/Public/HslCommunication.xml +++ b/Public/HslCommunication.xml @@ -706,6 +706,20 @@ + + + 获取数据流的md5码 + + 数据流,可以是内存流,也可以是文件流 + + + + + 获取内存图片的md5码 + + 内存图片 + + 从一个字节大小返回带单位的描述 @@ -2147,23 +2161,23 @@ - + - 基础下载文件信息 + 基础下载信息 服务器的文件名称 下载的进度报告 - 本地保存的文件名称,包含完整路径名 + 数据源信息,决定最终存储到哪里去 - + 上传文件给服务器 - 本地完整路径的文件名 + 数据源,可以是文件名,也可以是数据流 在服务器保存的文件名,不包含驱动器路径 @@ -2212,6 +2226,18 @@ 流数据 + + + 下载服务器的文件到本地的数据流中 + + 文件名称,带后缀 + 第一大类 + 第二大类 + 第三大类 + 下载的进度报告 + 内存文件 + + 上传本地的文件到服务器操作 @@ -2226,6 +2252,35 @@ 上传的进度报告 + + + 上传数据流到服务器操作 + + 数据流内容 + 服务器存储的文件名称,带后缀 + 第一大类 + 第二大类 + 第三大类 + 文件的额外描述 + 文件的上传人 + 上传的进度报告 + + + + + 上传内存图片到服务器操作 + + 内存图片,不能为空 + 服务器存储的文件名称,带后缀 + 第一大类 + 第二大类 + 第三大类 + 文件的额外描述 + 文件的上传人 + 上传的进度报告 + + + 根据三种分类信息,还原成在服务器的相对路径,包含文件 @@ -3900,6 +3955,20 @@ + + + [自校验] 将流数据发送至套接字,具体发送细节将在继承类中实现,如果结果异常,则结束通讯 + + 套接字 + 文件名称,文件必须存在 + 远程端的文件名称 + 文件的额外标签 + 文件的上传人 + 操作结果对象 + 发送进度报告 + + + [自校验] 接收一条完整的同步数据,包含头子节和内容字节,基础的数据,如果结果异常,则结束通讯 diff --git a/软件系统客户端Wpf/AppWpfHelper.cs b/软件系统客户端Wpf/AppWpfHelper.cs new file mode 100644 index 0000000..314a013 --- /dev/null +++ b/软件系统客户端Wpf/AppWpfHelper.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media.Imaging; + +namespace 软件系统客户端Wpf +{ + + + /*********************************************************************************** + * + * 说明:用于开发一些wpf专有的方法,一些转换方法 + * + ***********************************************************************************/ + + + + public class AppWpfHelper + { + public static BitmapImage TranslateImageToBitmapImage(System.Drawing.Bitmap bitmap) + { + System.IO.MemoryStream ms = new System.IO.MemoryStream(); + if(bitmap.RawFormat != null) bitmap.Save(ms, bitmap.RawFormat); + else bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); + BitmapImage bi = new BitmapImage(); + bi.BeginInit(); + bi.StreamSource = ms; + bi.EndInit(); + return bi; + } + + } +} diff --git a/软件系统客户端Wpf/MainWindow.xaml.cs b/软件系统客户端Wpf/MainWindow.xaml.cs index 40a5a33..acf1420 100644 --- a/软件系统客户端Wpf/MainWindow.xaml.cs +++ b/软件系统客户端Wpf/MainWindow.xaml.cs @@ -70,6 +70,8 @@ namespace 软件系统客户端Wpf public MainWindow() { InitializeComponent(); + + UserClient.PortraitManager = new UserPortrait(AppDomain.CurrentDomain.BaseDirectory); } #endregion @@ -112,10 +114,10 @@ namespace 软件系统客户端Wpf //窗口显示 IsWindowShow = true; - //udp测试 - //SendServerUdpData(0, "显示了窗体"); + // udp测试 + // SendServerUdpData(0, "显示了窗体"); - //是否显示更新日志,显示前进行判断该版本是否已经显示过了 + // 是否显示更新日志,显示前进行判断该版本是否已经显示过了 if (!UserClient.JsonSettings.IsNewVersionRunning) { UserClient.JsonSettings.IsNewVersionRunning = false; @@ -126,7 +128,7 @@ namespace 软件系统客户端Wpf - //根据权限使能菜单 + // 根据权限使能菜单 if(UserClient.UserAccount.Grade < AccountGrade.Admin) { MenuItem公告管理.IsEnabled = false; @@ -145,15 +147,15 @@ namespace 软件系统客户端Wpf } - //启动网络服务 + // 启动网络服务 Net_Socket_Client_Initialization(); - //启动定时器 + // 启动定时器 TimeTickInitilization(); - //显示头像 - SoftUserPortraitInitialization(); - SoftUserPortrait.LoadUserSmallPortraint(); + // 显示名称和加载头像 AccountChip.Content = UserClient.UserAccount.UserName; + AccountPortrait.Source = AppWpfHelper.TranslateImageToBitmapImage( + UserClient.PortraitManager.GetSmallPortraitByUserName(UserClient.UserAccount.UserName)); SetShowRenderControl(UIControl_Home); } @@ -328,7 +330,7 @@ namespace 软件系统客户端Wpf private void MenuItem我的信息_Click(object sender, RoutedEventArgs e) { - using (FormAccountDetails form = new FormAccountDetails(SoftUserPortrait)) + using (FormAccountDetails form = new FormAccountDetails()) { form.ShowDialog(); } @@ -403,8 +405,17 @@ namespace 软件系统客户端Wpf } } + private void AccountChip_Click(object sender, RoutedEventArgs e) + { + // 点击了头像,请求查看高清版本头像 + using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", UserClient.PortraitManager.ThreadPoolDownloadSizeLarge)) + { + fmr.ShowDialog(); + } + } + #endregion - + #region 异步网络块 private NetComplexClient net_socket_client = new NetComplexClient(); @@ -544,8 +555,14 @@ namespace 软件系统客户端Wpf } else if(customer == CommonHeadCode.MultiNetHeadCode.新头像更新) { + UserClient.PortraitManager.UpdateSmallPortraitByName(data); if (IsWindowShow) Dispatcher.Invoke(new Action(() => { + if (data == UserClient.UserAccount.UserName) + { + AccountPortrait.Source = AppWpfHelper.TranslateImageToBitmapImage( + UserClient.PortraitManager.GetSmallPortraitByUserName(data)); + } foreach (Views.Controls.UserClientRenderItem m in ClientsOnline.Children) { m.UpdatePortrait(data); @@ -694,50 +711,7 @@ namespace 软件系统客户端Wpf } #endregion - - #region 头像图片上传下载块 - - - private UserPortrait SoftUserPortrait { get; set; } - - private void SoftUserPortraitInitialization() - { - SoftUserPortrait = new UserPortrait(AppDomain.CurrentDomain.BaseDirectory + - @"Portrait\" + UserClient.UserAccount.UserName, ShowSmallPortrait); - } - - private void ShowSmallPortrait(System.Drawing.Bitmap bitmap) - { - if (IsWindowShow) Dispatcher.Invoke(new Action(() => - { - MemoryStream ms = new MemoryStream(); - bitmap.Save(ms, bitmap.RawFormat); - bitmap.Dispose(); - - BitmapImage bi = new BitmapImage(); - bi.BeginInit(); - bi.StreamSource = ms; - bi.EndInit(); - - AccountPortrait.Source = bi; - })); - } - - - - private void AccountChip_Click(object sender, RoutedEventArgs e) - { - // 点击了头像,请求查看高清版本头像 - using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", SoftUserPortrait.ThreadPoolDownloadSizeLarge)) - { - fmr.ShowDialog(); - } - } - - - #endregion - #region 多界面管理块 private List all_main_render = new List(); diff --git a/软件系统客户端Wpf/Views/Controls/UserClientRenderItem.xaml.cs b/软件系统客户端Wpf/Views/Controls/UserClientRenderItem.xaml.cs index 83dc67f..805c576 100644 --- a/软件系统客户端Wpf/Views/Controls/UserClientRenderItem.xaml.cs +++ b/软件系统客户端Wpf/Views/Controls/UserClientRenderItem.xaml.cs @@ -100,26 +100,11 @@ namespace 软件系统客户端Wpf.Views.Controls // 向服务器请求小头像 if (obj is NetAccount m_NetAccount) { - try + System.Drawing.Bitmap bitmap = UserClient.PortraitManager.GetSmallPortraitByUserName(m_NetAccount.UserName); + Dispatcher.Invoke(new Action(() => { - System.Drawing.Bitmap bitmap = UserPortrait.DownloadSmallPortraint(m_NetAccount.UserName); - MemoryStream ms = new MemoryStream(); - bitmap.Save(ms, bitmap.RawFormat); - bitmap.Dispose(); - - Dispatcher.Invoke(new Action(() => - { - BitmapImage bi = new BitmapImage(); - bi.BeginInit(); - bi.StreamSource = ms; - bi.EndInit(); - Image1.Source = bi; - })); - } - catch (Exception ex) - { - UserClient.LogNet?.WriteException("Thread Download Portrait Failed", ex); - } + Image1.Source = AppWpfHelper.TranslateImageToBitmapImage(bitmap); + })); } } diff --git a/软件系统客户端Wpf/软件系统客户端Wpf.csproj b/软件系统客户端Wpf/软件系统客户端Wpf.csproj index fbd331c..10bd162 100644 --- a/软件系统客户端Wpf/软件系统客户端Wpf.csproj +++ b/软件系统客户端Wpf/软件系统客户端Wpf.csproj @@ -74,6 +74,7 @@ MSBuild:Compile Designer + UserClientRenderItem.xaml diff --git a/软件系统客户端模版/FormMainWindow.cs b/软件系统客户端模版/FormMainWindow.cs index 0ac9ba2..1140e69 100644 --- a/软件系统客户端模版/FormMainWindow.cs +++ b/软件系统客户端模版/FormMainWindow.cs @@ -58,6 +58,9 @@ namespace 软件系统客户端模版 InitializeComponent(); Icon = UserClient.GetFormWindowIcon(); + + // 初始化头像管理器 + UserClient.PortraitManager = new UserPortrait(Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName); } #endregion @@ -139,12 +142,12 @@ namespace 软件系统客户端模版 // 启动网络服务 Net_Socket_Client_Initialization(); - // 启动头像 - SoftUserPortraitInitialization(); // 启动定时器 TimeTickInitilization(); + + // 显示头像 - SoftUserPortrait.LoadUserSmallPortraint(); + pictureBox1.Image = UserClient.PortraitManager.GetSmallPortraitByUserName(UserClient.UserAccount.UserName); } private void FormMainWindow_FormClosing(object sender, FormClosingEventArgs e) { @@ -327,7 +330,7 @@ namespace 软件系统客户端模版 private void 我的信息ToolStripMenuItem_Click(object sender, EventArgs e) { - using (FormAccountDetails form = new FormAccountDetails(SoftUserPortrait)) + using (FormAccountDetails form = new FormAccountDetails()) { form.ShowDialog(); } @@ -349,6 +352,15 @@ namespace 软件系统客户端模版 } } + private void pictureBox1_Click(object sender, EventArgs e) + { + //点击了头像,请求下载高清版本头像 + using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", UserClient.PortraitManager.ThreadPoolDownloadSizeLarge)) + { + fmr.ShowDialog(); + } + } + #endregion #region 异步网络块 @@ -468,8 +480,13 @@ namespace 软件系统客户端模版 } else if(customer == CommonHeadCode.MultiNetHeadCode.新头像更新) { + UserClient.PortraitManager.UpdateSmallPortraitByName(data); if (IsHandleCreated) Invoke(new Action(() => { + if(data == UserClient.UserAccount.UserName) + { + pictureBox1.Image = UserClient.PortraitManager.GetSmallPortraitByUserName(data); + } netClientOnline1.ClientUpdatePortrait(data); })); } @@ -724,59 +741,6 @@ namespace 软件系统客户端模版 - #endregion - - #region 头像图片上传下载块 - - - private UserPortrait SoftUserPortrait { get; set; } - - private void SoftUserPortraitInitialization() - { - SoftUserPortrait = new UserPortrait(Application.StartupPath + - @"\Portrait\" + UserClient.UserAccount.UserName, LoadSmallProtraitAsync); - } - - - private void LoadSmallProtraitAsync(Bitmap bitmap) - { - if (IsHandleCreated && InvokeRequired) - { - Invoke(new Action(() => - { - LoadSmallProtraitAsync(bitmap); - })); - return; - } - - pictureBox1.Image = bitmap; - } - - private void UnloadSmallProtrait() - { - if (IsHandleCreated && InvokeRequired) - { - Invoke(new Action(() => - { - UnloadSmallProtrait(); - })); - return; - } - - pictureBox1.Image = null; - } - - private void pictureBox1_Click(object sender, EventArgs e) - { - //点击了头像,请求下载高清版本头像 - using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", SoftUserPortrait.ThreadPoolDownloadSizeLarge)) - { - fmr.ShowDialog(); - } - } - - - #endregion