完成小头像管理器,优化我的信息头像加载器,v1.6.13

This commit is contained in:
dathlin
2017-10-14 21:21:33 +08:00
parent d43866b443
commit 2a3a0c5f98
12 changed files with 415 additions and 427 deletions

View File

@@ -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,49 +107,204 @@ 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);
// 先获取服务器端的MD5码
string fileServerMd5 = UserClient.UserAccount.LargePortraitMD5;
if (string.IsNullOrEmpty(fileServerMd5))
{
return; // 没有文件
}
private void UnloadLargeProtrait()
string fileName = Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName + @"\" + PortraitSupport.LargePortrait;
if(File.Exists(fileName))
{
if (IsHandleCreated && InvokeRequired)
bool loadSuccess = false;
Invoke(new Action(() =>
{
BeginInvoke(new Action(() =>
try
{
UnloadLargeProtrait();
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;
}
pictureBox_UserPortrait.Image = null;
}
private void LoadLargeProtrait(string fileName)
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 && InvokeRequired)
if(IsHandleCreated) Invoke(new Action(() =>
{
BeginInvoke(new Action(() =>
// 下载完成
Bitmap bitmap = new Bitmap(ms);
pictureBox_UserPortrait.Image = bitmap;
try
{
LoadLargeProtrait(fileName);
bitmap.Save(fileName);
}
catch
{
}
}));
return;
}
else
{
// 下载异常,丢弃
}
pictureBox_UserPortrait.ImageLocation = fileName;
ms.Dispose();
}
#endregion
#region Download FileNames

View File

@@ -26,7 +26,7 @@ namespace ClientsLibrary
/// <summary>
/// 头像管理
/// 头像管理器,负责所有小头像数据的缓存
/// </summary>
public class UserPortrait
{
@@ -37,7 +37,7 @@ namespace ClientsLibrary
/// </summary>
/// <param name="filePath">头像存储的文件夹路径</param>
/// <param name="loadPicSmall">加载头像的委托</param>
public UserPortrait(string filePath, Action<Bitmap> loadPicSmall)
public UserPortrait(string filePath)
{
if (!System.IO.Directory.Exists(filePath))
{
@@ -45,225 +45,6 @@ namespace ClientsLibrary
}
FileSavePath = filePath;
m_LoadPicSmall = loadPicSmall;
}
#endregion
#region Change Portrait
/// <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);
}
}
}
#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;
}
/// <summary>
/// 加载小尺寸的头像操作,需要放置到线程池中
/// </summary>
public void LoadUserSmallPortraint()
{
m_LoadPicSmall?.Invoke(DownloadSmallPortraint(UserClient.UserAccount.UserName));
}
/// <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);
}
}
#endregion
@@ -323,19 +104,62 @@ namespace ClientsLibrary
#endregion
#region Public Method
/// <summary>
///
/// </summary>
/// <returns></returns>
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);
}
public string GetSmallPortraitFileName()
hybirdLock.Leave();
}
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
@@ -347,10 +171,8 @@ namespace ClientsLibrary
/// </summary>
private string FileSavePath { get; set; }
/// <summary>
/// 加载小头像的委托
/// </summary>
private Action<Bitmap> m_LoadPicSmall { get; set; }
private Dictionary<string, Bitmap> dictSmallPortrait = new Dictionary<string, Bitmap>();
private SimpleHybirdLock hybirdLock = new SimpleHybirdLock();
#endregion
}

View File

@@ -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)
if(IsHandleCreated)
{
Invoke(new Action(() =>
{
Bitmap bitmap = new Bitmap(ms);
pictureBox1.Image = bitmap;
}
else
{
// 可能网络错误,也可能因为服务器没有头像
// MessageBox.Show(result.Message);
pictureBox1.Image = Properties.Resources.person_1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}));
}
}
}

View File

@@ -36,7 +36,7 @@ namespace ClientsLibrary
/// <summary>
/// 本软件的当前版本,用来验证更新的关键依据
/// </summary>
public static SystemVersion CurrentVersion { get; } = new SystemVersion("1.0.0.171013");
public static SystemVersion CurrentVersion { get; } = new SystemVersion("1.0.0.171014");
/// <summary>
@@ -133,8 +133,10 @@ namespace ClientsLibrary
return false;
}
/// <summary>
/// 头像管理器
/// </summary>
public static UserPortrait PortraitManager { get; set; }

View File

@@ -45,7 +45,7 @@ namespace CommonLibrary
*
**************************************************************************/
SoftBasic.FrameworkVersion = new SystemVersion("1.6.12");
SoftBasic.FrameworkVersion = new SystemVersion("1.6.13");
}

Binary file not shown.

View File

@@ -706,6 +706,20 @@
<param name="filePath"></param>
<returns></returns>
</member>
<member name="M:HslCommunication.BasicFramework.SoftBasic.CalculateStreamMD5(System.IO.Stream)">
<summary>
获取数据流的md5码
</summary>
<param name="stream">数据流,可以是内存流,也可以是文件流</param>
<returns></returns>
</member>
<member name="M:HslCommunication.BasicFramework.SoftBasic.CalculateStreamMD5(System.Drawing.Bitmap)">
<summary>
获取内存图片的md5码
</summary>
<param name="bitmap">内存图片</param>
<returns></returns>
</member>
<member name="M:HslCommunication.BasicFramework.SoftBasic.GetSizeDescription(System.Int64)">
<summary>
从一个字节大小返回带单位的描述
@@ -2147,23 +2161,23 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:HslCommunication.Enthernet.FileClientBase.DownloadFileBase(System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64},System.String)">
<member name="M:HslCommunication.Enthernet.FileClientBase.DownloadFileBase(System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64},System.Object)">
<summary>
基础下载文件信息
基础下载信息
</summary>
<param name="factory"></param>
<param name="group"></param>
<param name="id"></param>
<param name="fileName">服务器的文件名称</param>
<param name="processReport">下载的进度报告</param>
<param name="fileSaveName">本地保存的文件名称,包含完整路径名</param>
<param name="source">数据源信息,决定最终存储到哪里去</param>
<returns></returns>
</member>
<member name="M:HslCommunication.Enthernet.FileClientBase.UploadFileBase(System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})">
<member name="M:HslCommunication.Enthernet.FileClientBase.UploadFileBase(System.Object,System.String,System.String,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})">
<summary>
上传文件给服务器
</summary>
<param name="fileName">本地完整路径的文件名</param>
<param name="source">数据源,可以是文件名,也可以是数据流</param>
<param name="serverName">在服务器保存的文件名,不包含驱动器路径</param>
<param name="factory"></param>
<param name="group"></param>
@@ -2212,6 +2226,18 @@
<param name="stream">流数据</param>
<returns></returns>
</member>
<member name="M:HslCommunication.Enthernet.IntegrationFileClient.DownloadFile(System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64},System.Drawing.Bitmap@)">
<summary>
下载服务器的文件到本地的数据流中
</summary>
<param name="fileName">文件名称,带后缀</param>
<param name="factory">第一大类</param>
<param name="group">第二大类</param>
<param name="id">第三大类</param>
<param name="processReport">下载的进度报告</param>
<param name="bitmap">内存文件</param>
<returns></returns>
</member>
<member name="M:HslCommunication.Enthernet.IntegrationFileClient.UploadFile(System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})">
<summary>
上传本地的文件到服务器操作
@@ -2226,6 +2252,35 @@
<param name="processReport">上传的进度报告</param>
<returns></returns>
</member>
<member name="M:HslCommunication.Enthernet.IntegrationFileClient.UploadFile(System.IO.Stream,System.String,System.String,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})">
<summary>
上传数据流到服务器操作
</summary>
<param name="stream">数据流内容</param>
<param name="serverName">服务器存储的文件名称,带后缀</param>
<param name="factory">第一大类</param>
<param name="group">第二大类</param>
<param name="id">第三大类</param>
<param name="fileTag">文件的额外描述</param>
<param name="fileUpload">文件的上传人</param>
<param name="processReport">上传的进度报告</param>
<returns></returns>
</member>
<member name="M:HslCommunication.Enthernet.IntegrationFileClient.UploadFile(System.Drawing.Bitmap,System.String,System.String,System.String,System.String,System.String,System.String,System.Action{System.Int64,System.Int64})">
<summary>
上传内存图片到服务器操作
</summary>
<param name="bitmap">内存图片,不能为空</param>
<param name="serverName">服务器存储的文件名称,带后缀</param>
<param name="factory">第一大类</param>
<param name="group">第二大类</param>
<param name="id">第三大类</param>
<param name="fileTag">文件的额外描述</param>
<param name="fileUpload">文件的上传人</param>
<param name="processReport">上传的进度报告</param>
<returns></returns>
<exception cref="T:System.ArgumentNullException"></exception>
</member>
<member name="M:HslCommunication.Enthernet.IntegrationFileClient.TranslateFileName(System.String,System.String,System.String,System.String)">
<summary>
根据三种分类信息,还原成在服务器的相对路径,包含文件
@@ -3900,6 +3955,20 @@
<param name="failedString"></param>
<returns></returns>
</member>
<member name="M:HslCommunication.NetShareBase.SendFileAndCheckReceive(System.Net.Sockets.Socket,System.IO.Stream,System.String,System.String,System.String,HslCommunication.OperateResult,System.Action{System.Int64,System.Int64},System.String)">
<summary>
[自校验] 将流数据发送至套接字,具体发送细节将在继承类中实现,如果结果异常,则结束通讯
</summary>
<param name="socket">套接字</param>
<param name="stream">文件名称,文件必须存在</param>
<param name="servername">远程端的文件名称</param>
<param name="filetag">文件的额外标签</param>
<param name="fileupload">文件的上传人</param>
<param name="result">操作结果对象</param>
<param name="sendReport">发送进度报告</param>
<param name="failedString"></param>
<returns></returns>
</member>
<member name="M:HslCommunication.NetShareBase.ReceiveAndCheckBytes(System.Net.Sockets.Socket,System.Byte[]@,System.Byte[]@,HslCommunication.OperateResult,System.Action{System.Int64,System.Int64},System.String)">
<summary>
[自校验] 接收一条完整的同步数据,包含头子节和内容字节,基础的数据,如果结果异常,则结束通讯

View File

@@ -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;
}
}
}

View File

@@ -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,6 +405,15 @@ namespace 软件系统客户端Wpf
}
}
private void AccountChip_Click(object sender, RoutedEventArgs e)
{
// 点击了头像,请求查看高清版本头像
using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", UserClient.PortraitManager.ThreadPoolDownloadSizeLarge))
{
fmr.ShowDialog();
}
}
#endregion
#region
@@ -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);
@@ -693,49 +710,6 @@ namespace 软件系统客户端Wpf
UserClient.Net_Udp_Client.SendMessage(customer, data);
}
#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

View File

@@ -100,27 +100,12 @@ namespace 软件系统客户端Wpf.Views.Controls
// 向服务器请求小头像
if (obj is NetAccount m_NetAccount)
{
try
{
System.Drawing.Bitmap bitmap = UserPortrait.DownloadSmallPortraint(m_NetAccount.UserName);
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, bitmap.RawFormat);
bitmap.Dispose();
System.Drawing.Bitmap bitmap = UserClient.PortraitManager.GetSmallPortraitByUserName(m_NetAccount.UserName);
Dispatcher.Invoke(new Action(() =>
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
Image1.Source = bi;
Image1.Source = AppWpfHelper.TranslateImageToBitmapImage(bitmap);
}));
}
catch (Exception ex)
{
UserClient.LogNet?.WriteException("Thread Download Portrait Failed", ex);
}
}
}
#endregion

View File

@@ -74,6 +74,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="AppWpfHelper.cs" />
<Compile Include="Views\AnotherCommandImplementation.cs" />
<Compile Include="Views\Controls\UserClientRenderItem.xaml.cs">
<DependentUpon>UserClientRenderItem.xaml</DependentUpon>

View File

@@ -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