修复头像显示偶尔出现的异常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; }
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)
{
UserPortrait.ChangePortrait();
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolLoadLargePortrait), null);
UserPortrait.ChangePortrait(LoadLargeProtrait,UnloadLargeProtrait);
}
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
#region
@@ -240,6 +245,9 @@ namespace ClientsLibrary
#endregion
#region
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
@@ -299,5 +307,6 @@ namespace ClientsLibrary
}
#endregion
}
}

View File

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

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码
*
**************************************************************************************************/
************************************************************************************************/
/// <summary>
@@ -75,9 +75,13 @@ namespace CommonLibrary
//======================================================================================
// 此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上
// 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号
/************************************************************************************************
*
* 注意:此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上
* 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号
*
************************************************************************************************/
/// <summary>
/// 主网络端口,此处随机定义了一个数据
@@ -92,10 +96,6 @@ namespace CommonLibrary
/// </summary>
public static int Port_Update_Net { get; } = 17538;
/// <summary>
/// 用于软件远程更新的端口,此处随机定义了一个数据
/// </summary>
public static int Port_Update_Remote { get; } = 26435;
/// <summary>
/// 共享文件的端口号
/// </summary>
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,
m => {
byte[] content = System.IO.File.ReadAllBytes(m);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new System.IO.MemoryStream(content);
bi.EndInit();
AccountPortrait.Source = bi;
if (IsWindowShow) Dispatcher.Invoke(new Action(() =>
{
byte[] content = File.ReadAllBytes(m);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(content);
bi.EndInit();
AccountPortrait.Source = bi;
}));
},
()=>
{
AccountPortrait.Source = null;
if (IsWindowShow) Dispatcher.Invoke(new Action(() => {
AccountPortrait.Source = null;
}));
});
}
private void AccountChip_Click(object sender, RoutedEventArgs e)
{
//点击了头像,请求下载高清版本头像
// 点击了头像,请求查看高清版本头像
using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", SoftUserPortrait.ThreadPoolDownloadSizeLarge))
{
fmr.ShowDialog();

View File

@@ -18,9 +18,9 @@ using HslCommunication.LogNet;
/***************************************************************************************
*
* 模版日期 2017-09-02
* 创建人 胡少林
* 版权所有 胡少林
* 模版日期 2017-09-20
* 创建人 Richard.Hu
* 版权所有 Richard.Hu
* 授权说明 模版仅授权个人使用如需商用请联系hsl200909@163.com洽谈
* 说明一 JSON组件引用自james newton-king遵循MIT授权协议
* 说明二 文件的图标来源于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)
{
@@ -653,11 +649,39 @@ namespace 软件系统客户端模版
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)
{