优化图像更新流程:使用_isPaused标志管理暂停状态,统一Info显示逻辑,实现完整的三步绘制流程
This commit is contained in:
@@ -79,38 +79,41 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停/恢复图像更新菜单项点击事件处理
|
||||
/// 更新InfoImage显示
|
||||
/// 1. 如果断开,显示重连信息
|
||||
/// 2. 否则如果暂停,显示暂停信息
|
||||
/// 3. 最后调用更新UI
|
||||
/// </summary>
|
||||
private void PauseImageUpdateToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
private void UpdateInfo()
|
||||
{
|
||||
if (DesignMode) return;
|
||||
|
||||
try
|
||||
{
|
||||
bool isPaused = pauseImageUpdateToolStripMenuItem.Text == "暂停图像更新";
|
||||
|
||||
if (isPaused)
|
||||
lock (_infoImageLock)
|
||||
{
|
||||
pauseImageUpdateToolStripMenuItem.Text = "恢复图像更新";
|
||||
Console.WriteLine("图像更新已暂停");
|
||||
|
||||
// 在InfoImage中绘制暂停字样
|
||||
lock (_infoImageLock)
|
||||
// 释放之前的InfoImage资源
|
||||
if (_infoImage != null)
|
||||
{
|
||||
// 释放之前的InfoImage资源
|
||||
if (_infoImage != null)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_infoImage.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
|
||||
}
|
||||
_infoImage.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
|
||||
}
|
||||
_infoImage = null;
|
||||
}
|
||||
|
||||
// 创建新的InfoImage并绘制暂停字样
|
||||
// 检查连接状态
|
||||
bool isDisconnected = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Disconnected;
|
||||
bool isReconnecting = _deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Reconnecting;
|
||||
bool isPaused = _isPaused; // 使用_isPaused标志判断暂停状态
|
||||
|
||||
// 如果需要显示信息,创建InfoImage
|
||||
if (isDisconnected || isReconnecting || isPaused)
|
||||
{
|
||||
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||
using (Graphics g = Graphics.FromImage(_infoImage))
|
||||
{
|
||||
@@ -120,61 +123,99 @@ namespace JoyD.Windows.CS.Toprie
|
||||
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||
}
|
||||
|
||||
// 绘制暂停字样
|
||||
// 确定显示的文本和颜色
|
||||
string text = "";
|
||||
Color textColor = Color.White;
|
||||
|
||||
if (isDisconnected || isReconnecting)
|
||||
{
|
||||
// 断开或重连状态
|
||||
text = isReconnecting ? "正在重连..." : "连接断开";
|
||||
textColor = isReconnecting ? Color.Yellow : Color.Red;
|
||||
}
|
||||
else if (isPaused)
|
||||
{
|
||||
// 暂停状态
|
||||
text = "暂停";
|
||||
textColor = Color.Red;
|
||||
}
|
||||
|
||||
// 绘制文本
|
||||
using (Font font = new Font("Arial", 48, FontStyle.Bold))
|
||||
using (SolidBrush textBrush = new SolidBrush(Color.Red))
|
||||
using (SolidBrush textBrush = new SolidBrush(textColor))
|
||||
{
|
||||
StringFormat format = new StringFormat();
|
||||
format.Alignment = StringAlignment.Center;
|
||||
format.LineAlignment = StringAlignment.Center;
|
||||
g.DrawString("暂停", font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
||||
g.DrawString(text, font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置显示信息标志并更新UI
|
||||
_isDisplayingInfo = true;
|
||||
UpdateImageOnUI();
|
||||
// 设置显示标志
|
||||
_isDisplayingInfo = (isDisconnected || isReconnecting || isPaused);
|
||||
}
|
||||
else
|
||||
|
||||
// 调用更新UI
|
||||
UpdateImageOnUI();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"更新Info显示时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停/恢复图像更新菜单项点击事件处理
|
||||
/// 1、暂停或恢复时,设置暂停状态,调用更新Info
|
||||
/// </summary>
|
||||
private void PauseImageUpdateToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (DesignMode) return;
|
||||
|
||||
try
|
||||
{
|
||||
// 切换暂停状态
|
||||
_isPaused = !_isPaused;
|
||||
|
||||
if (_isPaused)
|
||||
{
|
||||
pauseImageUpdateToolStripMenuItem.Text = "暂停图像更新";
|
||||
Console.WriteLine("图像更新已恢复");
|
||||
// 设置暂停状态
|
||||
pauseImageUpdateToolStripMenuItem.Text = "恢复图像更新";
|
||||
|
||||
// 清除InfoImage和显示标志
|
||||
lock (_infoImageLock)
|
||||
{
|
||||
if (_infoImage != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_infoImage.Dispose();
|
||||
_infoImage = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"释放InfoImage资源异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
_isDisplayingInfo = false;
|
||||
|
||||
// 立即更新UI
|
||||
UpdateImageOnUI();
|
||||
|
||||
// 恢复时,立即停止并重新开始图像接收,确保获取最新图像
|
||||
// 暂停时停止图像接收
|
||||
if (_isReceivingImage && _deviceManager != null)
|
||||
{
|
||||
_deviceManager.StopImageReceiving();
|
||||
_deviceManager.StartImageReceiving();
|
||||
_isReceivingImage = false;
|
||||
}
|
||||
|
||||
Console.WriteLine("图像更新已暂停");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 设置恢复状态
|
||||
pauseImageUpdateToolStripMenuItem.Text = "暂停图像更新";
|
||||
|
||||
// 恢复时,立即停止并重新开始图像接收,确保获取最新图像
|
||||
if (_deviceManager != null && _deviceManager.ConnectionStatus == ConnectionStatus.Connected)
|
||||
{
|
||||
_deviceManager.StopImageReceiving();
|
||||
_deviceManager.StartImageReceiving();
|
||||
_isReceivingImage = true;
|
||||
}
|
||||
|
||||
Console.WriteLine("图像更新已恢复");
|
||||
}
|
||||
|
||||
// 按照用户要求:暂停或恢复时,设置暂停状态,调用更新Info
|
||||
UpdateInfo();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"处理暂停/恢复图像更新时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Camera()
|
||||
{
|
||||
@@ -428,15 +469,13 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
}
|
||||
|
||||
private bool _isPaused = false; // 暂停状态标志
|
||||
|
||||
/// <summary>
|
||||
/// 设备管理器图像接收事件处理
|
||||
/// </summary>
|
||||
private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e)
|
||||
{
|
||||
// 如果图像更新已暂停,则不更新UI
|
||||
// 通过菜单项文本判断是否暂停
|
||||
if (pauseImageUpdateToolStripMenuItem.Text == "恢复图像更新")
|
||||
return;
|
||||
if (DesignMode) return;
|
||||
|
||||
try
|
||||
@@ -475,7 +514,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
return;
|
||||
}
|
||||
|
||||
// 按照用户要求:收到图像数据后,将图像保存到LastImage中,调用更新
|
||||
// 按照用户要求:收到图像数据后,将图像保存到LastImage中
|
||||
lock (_lastImageLock)
|
||||
{
|
||||
// 释放旧的LastImage资源
|
||||
@@ -487,18 +526,22 @@ namespace JoyD.Windows.CS.Toprie
|
||||
_lastImage = newImage;
|
||||
}
|
||||
|
||||
// 调用更新UI
|
||||
this.BeginInvoke(new Action(() =>
|
||||
// 按照用户要求:调用更新到UI
|
||||
// 只有当图像更新未暂停时才更新UI
|
||||
if (!_isPaused)
|
||||
{
|
||||
try
|
||||
this.BeginInvoke(new Action(() =>
|
||||
{
|
||||
UpdateImageOnUI();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"更新UI图像失败: {ex.Message}");
|
||||
}
|
||||
}));
|
||||
try
|
||||
{
|
||||
UpdateImageOnUI();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"更新UI图像失败: {ex.Message}");
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -630,29 +673,6 @@ namespace JoyD.Windows.CS.Toprie
|
||||
g.DrawString("测试信息", font, textBrush, 10, 10);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否需要绘制暂停状态的mask信息
|
||||
bool isPaused = pauseImageUpdateToolStripMenuItem.Text == "恢复图像更新";
|
||||
if (isPaused)
|
||||
{
|
||||
// 绘制半透明黑色背景
|
||||
using (Brush brush = new SolidBrush(Color.FromArgb(100, Color.Black)))
|
||||
{
|
||||
g.FillRectangle(brush, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||
}
|
||||
|
||||
// 绘制"暂停"文字
|
||||
using (Font font = new Font("微软雅黑", 48, FontStyle.Bold))
|
||||
using (Brush textBrush = new SolidBrush(Color.White))
|
||||
{
|
||||
StringFormat format = new StringFormat();
|
||||
format.Alignment = StringAlignment.Center;
|
||||
format.LineAlignment = StringAlignment.Center;
|
||||
|
||||
g.DrawString("暂停", font, textBrush,
|
||||
new Rectangle(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -780,6 +800,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
|
||||
/// <summary>
|
||||
/// 处理连接状态变更
|
||||
/// 2、断开或连接时,设置连接状态,调用更新Info
|
||||
/// </summary>
|
||||
private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e)
|
||||
{
|
||||
@@ -808,10 +829,6 @@ namespace JoyD.Windows.CS.Toprie
|
||||
case ConnectionStatus.Connected:
|
||||
Console.WriteLine("设备已连接");
|
||||
|
||||
// 清除显示标志并刷新UI
|
||||
_isDisplayingInfo = false;
|
||||
UpdateImageOnUI();
|
||||
|
||||
// 仅在首次连接时设置为热图模式,重连时保留之前的模式
|
||||
if (!_isReceivingImage) // 首次连接时_isReceivingImage为false
|
||||
{
|
||||
@@ -836,8 +853,18 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 开始接收图像(包含在try-catch中)
|
||||
if (!_isReceivingImage)
|
||||
{
|
||||
StartReceiveImage();
|
||||
try
|
||||
{
|
||||
StartReceiveImage();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"开始接收图像失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 设置连接状态后,调用更新Info
|
||||
UpdateInfo();
|
||||
break;
|
||||
case ConnectionStatus.Disconnected:
|
||||
Console.WriteLine("设备已断开连接");
|
||||
@@ -857,39 +884,8 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
}
|
||||
|
||||
// 在InfoImage中显示连接断开信息
|
||||
lock (_infoImageLock)
|
||||
{
|
||||
// 如果_infoImage为null,则创建新对象
|
||||
if (_infoImage == null)
|
||||
{
|
||||
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||
}
|
||||
|
||||
// 直接在现有InfoImage上重绘,不需要Dispose
|
||||
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);
|
||||
}
|
||||
|
||||
// 绘制断开连接字样
|
||||
using (Font font = new Font("Arial", 36, FontStyle.Bold))
|
||||
using (SolidBrush textBrush = new SolidBrush(Color.Red))
|
||||
{
|
||||
StringFormat format = new StringFormat();
|
||||
format.Alignment = StringAlignment.Center;
|
||||
format.LineAlignment = StringAlignment.Center;
|
||||
g.DrawString("连接断开", font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置显示信息标志并更新UI
|
||||
_isDisplayingInfo = true;
|
||||
UpdateImageOnUI();
|
||||
// 设置连接状态后,调用更新Info
|
||||
UpdateInfo();
|
||||
|
||||
if (!string.IsNullOrEmpty(e.DeviceInfo))
|
||||
{
|
||||
@@ -908,39 +904,8 @@ namespace JoyD.Windows.CS.Toprie
|
||||
Console.WriteLine($"正在重新连接设备...{(!string.IsNullOrEmpty(e.DeviceInfo) ? " " + e.DeviceInfo : "")}");
|
||||
ShowError(string.Empty); // 清除之前的错误信息
|
||||
|
||||
// 在InfoImage中显示正在重连信息
|
||||
lock (_infoImageLock)
|
||||
{
|
||||
// 如果_infoImage为null,则创建新对象
|
||||
if (_infoImage == null)
|
||||
{
|
||||
_infoImage = new Bitmap(BUFFER_WIDTH, BUFFER_HEIGHT);
|
||||
}
|
||||
|
||||
// 直接在现有InfoImage上重绘,不需要Dispose
|
||||
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);
|
||||
}
|
||||
|
||||
// 绘制正在重连字样
|
||||
using (Font font = new Font("Arial", 36, FontStyle.Bold))
|
||||
using (SolidBrush textBrush = new SolidBrush(Color.Yellow))
|
||||
{
|
||||
StringFormat format = new StringFormat();
|
||||
format.Alignment = StringAlignment.Center;
|
||||
format.LineAlignment = StringAlignment.Center;
|
||||
g.DrawString("正在重连...", font, textBrush, new RectangleF(0, 0, BUFFER_WIDTH, BUFFER_HEIGHT), format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 设置显示信息标志并更新UI
|
||||
_isDisplayingInfo = true;
|
||||
UpdateImageOnUI();
|
||||
// 设置重连状态后,调用更新Info
|
||||
UpdateInfo();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user