修改InfoImage资源处理逻辑,从释放资源改为简单清理

This commit is contained in:
zqm
2025-10-30 14:39:24 +08:00
parent 5a6172d541
commit c525b938af

View File

@@ -81,9 +81,10 @@ namespace JoyD.Windows.CS.Toprie
/// <summary>
/// 更新InfoImage显示
/// 1. 如果断开,显示重连信息
/// 2. 否则如果暂停,显示暂停信息
/// 3. 最后调用更新UI
/// 1. 如果暂停,显示暂停信息
/// 2. 否则如果Ping不通或断开,显示重连信息
/// 3. 否则清空InfoImage
/// 4. 最后调用更新UI
/// </summary>
private void UpdateInfo()
{
@@ -95,17 +96,9 @@ namespace JoyD.Windows.CS.Toprie
{
lock (_infoImageLock)
{
// 释放之前的InfoImage资源
// 清理InfoImage
if (_infoImage != null)
{
try
{
_infoImage.Dispose();
}
catch (Exception ex)
{
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
}
_infoImage = null;
}
@@ -115,9 +108,39 @@ namespace JoyD.Windows.CS.Toprie
bool isPaused = _isPaused; // 使用_isPaused标志判断暂停状态
bool isPingFailed = !IsDevicePingable;
// 如果需要显示信息创建InfoImage
if (isDisconnected || isReconnecting || isPaused || isPingFailed)
// 根据用户要求的优先级显示信息先检查暂停状态然后再检查Ping状态和连接状态
if (isPaused)
{
// 暂停状态 - 最高优先级
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
using (Graphics g = Graphics.FromImage(_infoImage))
{
// 设置半透明背景
using (SolidBrush brush = new SolidBrush(Color.FromArgb(128, Color.Black)))
{
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
}
// 绘制暂停文本
string text = "暂停";
Color textColor = Color.Red;
using (Font font = new Font("Arial", 48, FontStyle.Bold))
using (SolidBrush textBrush = new SolidBrush(textColor))
{
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
// 将主文本居中显示
g.DrawString(text, font, textBrush,
new RectangleF(0, BUFFER_HEIGHT / 3, BUFFER_WIDTH, BUFFER_HEIGHT / 3),
format);
}
}
}
else if (isPingFailed || isDisconnected || isReconnecting)
{
// 非暂停状态下检查Ping状态和连接状态
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
using (Graphics g = Graphics.FromImage(_infoImage))
{
@@ -131,10 +154,6 @@ namespace JoyD.Windows.CS.Toprie
string text = "";
Color textColor = Color.White;
// 根据要求的优先级显示信息先检查Ping状态和连接状态最后检查暂停状态
if (isPingFailed || isDisconnected || isReconnecting)
{
// Ping不通或断开连接状态
if (isReconnecting)
{
text = "正在重连...";
@@ -150,13 +169,6 @@ namespace JoyD.Windows.CS.Toprie
text = "连接断开";
textColor = Color.Red;
}
}
else if (isPaused)
{
// 暂停状态
text = "暂停";
textColor = Color.Red;
}
// 绘制文本
using (Font font = new Font("Arial", 48, FontStyle.Bold))
@@ -172,9 +184,10 @@ namespace JoyD.Windows.CS.Toprie
}
}
}
// 否则清空InfoImage已在开头处理
// 设置显示标志
_isDisplayingInfo = (isDisconnected || isReconnecting || isPaused);
_isDisplayingInfo = (isPaused || isDisconnected || isReconnecting);
}
// 调用更新UI