修复头像显示偶尔出现的异常BUG,v1.4.3

This commit is contained in:
dathlin
2017-09-20 21:55:43 +08:00
parent 51ed4031c5
commit e12ebd7416
5 changed files with 156 additions and 85 deletions

View File

@@ -71,42 +71,47 @@ namespace ClientsLibrary
public UserPortrait UserPortrait { get; } public UserPortrait UserPortrait { get; }
private void LoadPortraitByFileName(string fileName)
{
if(IsHandleCreated)
{
if(InvokeRequired)
{
BeginInvoke(new Action(() =>
{
LoadPortraitByFileName(fileName);
}));
return;
}
try
{
pictureBox_UserPortrait.LoadAsync(fileName);
}
catch(Exception ex)
{
UserClient.LogNet?.WriteException("加载图片异常!", ex);
}
}
}
private void pictureBox_UserPortrait_Click(object sender, EventArgs e) private void pictureBox_UserPortrait_Click(object sender, EventArgs e)
{ {
UserPortrait.ChangePortrait(); UserPortrait.ChangePortrait(LoadLargeProtrait,UnloadLargeProtrait);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolLoadLargePortrait), null);
} }
private void ThreadPoolLoadLargePortrait(object obj) private void ThreadPoolLoadLargePortrait(object obj)
{ {
UserPortrait.LoadUserLargePortraint(LoadPortraitByFileName); Thread.Sleep(200);
UserPortrait.LoadUserLargePortraint(LoadLargeProtrait, UnloadLargeProtrait);
} }
private void UnloadLargeProtrait()
{
if (IsHandleCreated && InvokeRequired)
{
BeginInvoke(new Action(() =>
{
UnloadLargeProtrait();
}));
return;
}
pictureBox_UserPortrait.Image = null;
}
private void LoadLargeProtrait(string fileName)
{
if (IsHandleCreated && InvokeRequired)
{
BeginInvoke(new Action(() =>
{
LoadLargeProtrait(fileName);
}));
return;
}
pictureBox_UserPortrait.ImageLocation = fileName;
}
#endregion #endregion
#region #region
@@ -240,6 +245,9 @@ namespace ClientsLibrary
#endregion #endregion
#region
private void treeView1_DragDrop(object sender, DragEventArgs e) private void treeView1_DragDrop(object sender, DragEventArgs e)
{ {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) if (e.Data.GetDataPresent(DataFormats.FileDrop))
@@ -299,5 +307,6 @@ namespace ClientsLibrary
} }
#endregion
} }
} }

View File

@@ -35,17 +35,17 @@ namespace ClientsLibrary
/// 实例化一个新的头像管理类对象 /// 实例化一个新的头像管理类对象
/// </summary> /// </summary>
/// <param name="filePath">头像存储的文件夹路径</param> /// <param name="filePath">头像存储的文件夹路径</param>
/// <param name="loadPic">加载图片的方法</param> /// <param name="unloadPicSmall">卸载小头像的委托</param>
/// <param name="disPic">加载前的操作</param> public UserPortrait(string filePath,Action<string> loadPicSmall, Action unloadPicSmall)
public UserPortrait(string filePath, Action<string> loadPic, Action disPic)
{ {
if (!System.IO.Directory.Exists(filePath)) if (!System.IO.Directory.Exists(filePath))
{ {
System.IO.Directory.CreateDirectory(filePath); System.IO.Directory.CreateDirectory(filePath);
} }
FileSavePath = filePath; FileSavePath = filePath;
LoadPic = loadPic;
DisPic = disPic; m_UnloadPicSmall = unloadPicSmall;
m_LoadPicSmall = loadPicSmall;
} }
#endregion #endregion
@@ -55,7 +55,9 @@ namespace ClientsLibrary
/// <summary> /// <summary>
/// 点击更改头像后的操作打开头像选择对话框获取到2种分辨率的头像然后进行上传 /// 点击更改头像后的操作打开头像选择对话框获取到2种分辨率的头像然后进行上传
/// </summary> /// </summary>
public void ChangePortrait() /// <param name="loadPic">加载大头像的委托</param>
/// <param name="unloadPic">卸载大头像的委托</param>
public void ChangePortrait(Action<string> loadPic, Action unloadPic)
{ {
using (FormPortraitSelect fps = new FormPortraitSelect()) using (FormPortraitSelect fps = new FormPortraitSelect())
{ {
@@ -64,14 +66,31 @@ namespace ClientsLibrary
string path300 = FileSavePath + @"\" + PortraitSupport.LargePortrait; string path300 = FileSavePath + @"\" + PortraitSupport.LargePortrait;
string path32 = FileSavePath + @"\" + PortraitSupport.SmallPortrait; string path32 = FileSavePath + @"\" + PortraitSupport.SmallPortrait;
DisPic?.Invoke(); // 先卸载图片
unloadPic?.Invoke();
m_UnloadPicSmall?.Invoke();
Bitmap bitmap300 = fps.GetSpecifiedSizeImage(300); Bitmap bitmap300 = fps.GetSpecifiedSizeImage(300);
bitmap300.Save(path300);
Bitmap bitmap32 = fps.GetSpecifiedSizeImage(32); Bitmap bitmap32 = fps.GetSpecifiedSizeImage(32);
try
{
bitmap300.Save(path300);
bitmap32.Save(path32); bitmap32.Save(path32);
bitmap300.Dispose(); bitmap300.Dispose();
bitmap32.Dispose(); bitmap32.Dispose();
}
catch(Exception ex)
{
// 文件被占用的时候无法进行覆盖
UserClient.LogNet?.WriteException("头像保存失败!", ex);
MessageBox.Show("头像保存失败,原因:" + ex.Message);
// 加载回旧的文件
loadPic?.Invoke(path300);
m_LoadPicSmall?.Invoke(path32);
return;
}
// 传送服务器 // 传送服务器
@@ -123,8 +142,6 @@ namespace ClientsLibrary
{ {
UserClient.UserAccount.SmallPortraitMD5 = SmallPortraitMD5; UserClient.UserAccount.SmallPortraitMD5 = SmallPortraitMD5;
UserClient.UserAccount.LargePortraitMD5 = LargePortraitMD5; UserClient.UserAccount.LargePortraitMD5 = LargePortraitMD5;
// 成功上传MD5码
LoadUserSmallPortraint();
} }
else else
{ {
@@ -136,6 +153,10 @@ namespace ClientsLibrary
MessageBox.Show("上传头像失败!原因:" + result.Message); MessageBox.Show("上传头像失败!原因:" + result.Message);
} }
// 先显示信息
loadPic?.Invoke(path300);
m_LoadPicSmall?.Invoke(path32);
}), null); }), null);
} }
@@ -149,7 +170,7 @@ namespace ClientsLibrary
#region Load Portraint #region Load Portraint
/// <summary> /// <summary>
/// 加载小尺寸的头像操作,可以放到主线程 /// 加载小尺寸的头像操作,需要放置到线程池中
/// </summary> /// </summary>
public void LoadUserSmallPortraint() public void LoadUserSmallPortraint()
{ {
@@ -165,18 +186,20 @@ namespace ClientsLibrary
string fileName = FileSavePath + @"\" + PortraitSupport.SmallPortrait; string fileName = FileSavePath + @"\" + PortraitSupport.SmallPortrait;
if (System.IO.File.Exists(fileName)) if (System.IO.File.Exists(fileName))
{ {
// 先进行加载
m_LoadPicSmall?.Invoke(fileName);
// 本地存在文件 // 本地存在文件
string currentMd5 = SoftBasic.CalculateFileMD5(fileName); string currentMd5 = SoftBasic.CalculateFileMD5(fileName);
// 对比验证 // 对比验证
if (fileName == currentMd5) if (fileName == currentMd5)
{ {
// 加载本地文件
LoadPic?.Invoke(fileName);
return; return;
} }
} }
m_UnloadPicSmall?.Invoke();
// 本地不存在文件或校验失败,需要重新下载 // 本地不存在文件或校验失败,需要重新下载
OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.SmallPortrait, OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.SmallPortrait,
"Files", "Files",
@@ -188,7 +211,7 @@ namespace ClientsLibrary
if(result.IsSuccess) if(result.IsSuccess)
{ {
// 下载成功 // 下载成功
LoadPic?.Invoke(fileName); m_LoadPicSmall?.Invoke(fileName);
} }
else else
{ {
@@ -201,7 +224,7 @@ namespace ClientsLibrary
/// 加载大尺寸的头像方法,需要放到线程池中操作 /// 加载大尺寸的头像方法,需要放到线程池中操作
/// </summary> /// </summary>
/// <param name="largeLoadAction"></param> /// <param name="largeLoadAction"></param>
public void LoadUserLargePortraint(Action<string> largeLoadAction) public void LoadUserLargePortraint(Action<string> largeLoadAction, Action largeUnloadAction)
{ {
// 先获取服务器端的MD5码 // 先获取服务器端的MD5码
string fileMd5 = UserClient.UserAccount.LargePortraitMD5; string fileMd5 = UserClient.UserAccount.LargePortraitMD5;
@@ -218,6 +241,8 @@ namespace ClientsLibrary
// 本地存在文件,先进行加载,如果运算不一致,再重新加载 // 本地存在文件,先进行加载,如果运算不一致,再重新加载
largeLoadAction?.Invoke(fileName); largeLoadAction?.Invoke(fileName);
string currentMd5 = null; string currentMd5 = null;
try try
{ {
currentMd5 = SoftBasic.CalculateFileMD5(fileName); currentMd5 = SoftBasic.CalculateFileMD5(fileName);
@@ -235,6 +260,8 @@ namespace ClientsLibrary
} }
} }
largeUnloadAction?.Invoke();
// 本地不存在文件或校验失败,需要重新下载 // 本地不存在文件或校验失败,需要重新下载
OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.LargePortrait, OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.LargePortrait,
"Files", "Files",
@@ -253,6 +280,7 @@ namespace ClientsLibrary
MessageBox.Show("头像从服务器下载失败,错误原因:" + result.Message); MessageBox.Show("头像从服务器下载失败,错误原因:" + result.Message);
} }
} }
#endregion #endregion
@@ -335,15 +363,16 @@ namespace ClientsLibrary
/// 文件的路径 /// 文件的路径
/// </summary> /// </summary>
private string FileSavePath { get; set; } private string FileSavePath { get; set; }
/// <summary>
/// 加载图片的操作
/// </summary>
private Action<string> LoadPic = null;
/// <summary>
/// 加载图片前的操作
/// </summary>
private Action DisPic = null;
/// <summary>
/// 卸载小头像的委托
/// </summary>
private Action m_UnloadPicSmall { get; set; }
/// <summary>
/// 加载小头像的委托
/// </summary>
private Action<string> m_LoadPicSmall { get; set; }
#endregion #endregion
} }

View File

@@ -55,7 +55,7 @@ namespace CommonLibrary
* *
**************************************************************************/ **************************************************************************/
SoftBasic.FrameworkVersion = new SystemVersion("1.4.2"); SoftBasic.FrameworkVersion = new SystemVersion("1.4.3");
} }
@@ -65,7 +65,7 @@ namespace CommonLibrary
* *
* 注意您在准备二次开发时应该重新生成一个自己的GUID码 * 注意您在准备二次开发时应该重新生成一个自己的GUID码
* *
**************************************************************************************************/ ************************************************************************************************/
/// <summary> /// <summary>
@@ -75,9 +75,13 @@ namespace CommonLibrary
//====================================================================================== /************************************************************************************************
// 此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上 *
// 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号 * 注意:此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上
* 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号
*
************************************************************************************************/
/// <summary> /// <summary>
/// 主网络端口,此处随机定义了一个数据 /// 主网络端口,此处随机定义了一个数据
@@ -92,10 +96,6 @@ namespace CommonLibrary
/// </summary> /// </summary>
public static int Port_Update_Net { get; } = 17538; public static int Port_Update_Net { get; } = 17538;
/// <summary> /// <summary>
/// 用于软件远程更新的端口,此处随机定义了一个数据
/// </summary>
public static int Port_Update_Remote { get; } = 26435;
/// <summary>
/// 共享文件的端口号 /// 共享文件的端口号
/// </summary> /// </summary>
public static int Port_Share_File { get; } = 34261; public static int Port_Share_File { get; } = 34261;

View File

@@ -621,23 +621,32 @@ namespace 软件系统客户端Wpf
{ {
SoftUserPortrait = new UserPortrait(AppDomain.CurrentDomain.BaseDirectory + @"Portrait\" + UserClient.UserAccount.UserName, SoftUserPortrait = new UserPortrait(AppDomain.CurrentDomain.BaseDirectory + @"Portrait\" + UserClient.UserAccount.UserName,
m => { m => {
byte[] content = System.IO.File.ReadAllBytes(m);
if (IsWindowShow) Dispatcher.Invoke(new Action(() =>
{
byte[] content = File.ReadAllBytes(m);
BitmapImage bi = new BitmapImage(); BitmapImage bi = new BitmapImage();
bi.BeginInit(); bi.BeginInit();
bi.StreamSource = new System.IO.MemoryStream(content); bi.StreamSource = new MemoryStream(content);
bi.EndInit(); bi.EndInit();
AccountPortrait.Source = bi; AccountPortrait.Source = bi;
}));
}, },
()=> ()=>
{ {
if (IsWindowShow) Dispatcher.Invoke(new Action(() => {
AccountPortrait.Source = null; AccountPortrait.Source = null;
}));
}); });
} }
private void AccountChip_Click(object sender, RoutedEventArgs e) private void AccountChip_Click(object sender, RoutedEventArgs e)
{ {
//点击了头像,请求下载高清版本头像 // 点击了头像,请求查看高清版本头像
using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", SoftUserPortrait.ThreadPoolDownloadSizeLarge)) using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", SoftUserPortrait.ThreadPoolDownloadSizeLarge))
{ {
fmr.ShowDialog(); fmr.ShowDialog();

View File

@@ -18,9 +18,9 @@ using HslCommunication.LogNet;
/*************************************************************************************** /***************************************************************************************
* *
* 模版日期 2017-09-02 * 模版日期 2017-09-20
* 创建人 胡少林 * 创建人 Richard.Hu
* 版权所有 胡少林 * 版权所有 Richard.Hu
* 授权说明 模版仅授权个人使用如需商用请联系hsl200909@163.com洽谈 * 授权说明 模版仅授权个人使用如需商用请联系hsl200909@163.com洽谈
* 说明一 JSON组件引用自james newton-king遵循MIT授权协议 * 说明一 JSON组件引用自james newton-king遵循MIT授权协议
* 说明二 文件的图标来源于http://fileicons.chromefans.org/,感谢作者的无私分享 * 说明二 文件的图标来源于http://fileicons.chromefans.org/,感谢作者的无私分享
@@ -302,10 +302,6 @@ namespace 软件系统客户端模版
} }
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
SoftUserPortrait.ChangePortrait();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e) private void ToolStripMenuItem_Click(object sender, EventArgs e)
{ {
@@ -653,11 +649,39 @@ namespace 软件系统客户端模版
private void SoftUserPortraitInitialization() private void SoftUserPortraitInitialization()
{ {
SoftUserPortrait = new UserPortrait(Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName, m => pictureBox1.LoadAsync(m),null); SoftUserPortrait = new UserPortrait(
Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName,
LoadSmallProtraitAsync, UnloadSmallProtrait);
} }
private void LoadSmallProtraitAsync(string fileName)
{
if (IsHandleCreated && InvokeRequired)
{
Invoke(new Action(() =>
{
LoadSmallProtraitAsync(fileName);
}));
return;
}
pictureBox1.LoadAsync(fileName);
}
private void UnloadSmallProtrait()
{
if (IsHandleCreated && InvokeRequired)
{
Invoke(new Action(() =>
{
UnloadSmallProtrait();
}));
return;
}
pictureBox1.Image = null;
}
private void pictureBox1_Click(object sender, EventArgs e) private void pictureBox1_Click(object sender, EventArgs e)
{ {