修复图像参数无效异常:在UpdatePictureBoxImage方法中添加图像有效性检查,防止无效图像导致的System.ArgumentException异常
This commit is contained in:
@@ -89,11 +89,30 @@ namespace JoyD.Windows.CS
|
||||
// 保存旧图像引用,以便设置新图像后释放
|
||||
Image oldImage = pictureBoxTemperatureDisplay.Image;
|
||||
|
||||
// 设置新图像
|
||||
pictureBoxTemperatureDisplay.Image = mRealTimeImage;
|
||||
// 检查mRealTimeImage是否有效 - 只设置有效的图像
|
||||
Image imageToSet = null;
|
||||
try
|
||||
{
|
||||
// 验证图像是否有效(尝试访问图像属性,如果无效会抛出异常)
|
||||
if (mRealTimeImage != null)
|
||||
{
|
||||
// 尝试访问Width属性来验证图像是否有效
|
||||
var width = mRealTimeImage.Width;
|
||||
// 如果没有抛出异常,则图像有效
|
||||
imageToSet = mRealTimeImage;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// 图像无效,不设置它
|
||||
// 可以选择记录日志或创建一个默认的错误图像
|
||||
}
|
||||
|
||||
// 设置图像(如果有效)
|
||||
pictureBoxTemperatureDisplay.Image = imageToSet;
|
||||
|
||||
// 释放旧图像(如果不是当前设置的图像)
|
||||
if (oldImage != null && oldImage != mRealTimeImage)
|
||||
if (oldImage != null && oldImage != imageToSet)
|
||||
{
|
||||
try { oldImage.Dispose(); } catch { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user