You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#从List<Bitmap>分配图片时抛出"Parameter is not valid"异常

问题分析与解决方案

核心问题根源

  1. bitmap_list为空无数据:你明确提到bitmap_list没有数据,直接执行pb.Image = bitmap_list[bitmapindex]必然触发System.Argument.Exception,因为列表中不存在对应索引的Bitmap对象。
  2. DragDrop过程未将Bitmap存入bitmap_list:在sidepicturebox_DragDrop方法中,你克隆了Bitmap并赋值给PictureBox,但未将克隆后的Bitmap添加到bitmap_list,导致后续转移操作无数据可用。
  3. 索引越界风险:dicomfileindexlist[pbindex - 1]的取值可能超出bitmap_list的有效索引范围,即使后续存入数据,也需要做边界校验。

修复步骤

1. 完善DragDrop方法,将Bitmap存入bitmap_list

修改sidepicturebox_DragDrop,补充将克隆的Bitmap加入bitmap_list的逻辑,同时规范资源管理:

private void sidepicturebox_DragDrop(object sender, DragEventArgs e)
{
    PictureBox sidepicturebox = (PictureBox)sender;

    dragdrop = true;
    dragdrop_overlap = false;
    overlapindex = 0;
    dragdrop_picturebox = sidepicturebox; // 无需重新实例化,直接赋值

    // 先判断拖放数据是否有效
    var droppedImage = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
    if (droppedImage == null) return;

    // 克隆Bitmap
    Rectangle cloneRect = new Rectangle(0, 0, droppedImage.Width, droppedImage.Height);
    Bitmap clonebitmap = droppedImage.Clone(cloneRect, droppedImage.PixelFormat);

    // 清理旧图片资源,避免内存泄漏
    if (sidepicturebox.Image != null)
    {
        sidepicturebox.Image.Dispose();
        sidepicturebox.Image = null;
    }

    // 赋值新图片并存入bitmap_list
    sidepicturebox.Image = clonebitmap;
    bitmap_list.Add(clonebitmap);

    int pbindex = Convert.ToInt16(sidepicturebox.Name);
    if (dragdrop)
    { 
        Update_pbindexlist(pbindexlist, pbindex);  
    }            

    // 释放原始拖放的Bitmap资源
    droppedImage.Dispose();
}

2. 增强转移方法的健壮性,添加边界检查

修改TransferImagesBetweenFrames_Updatepbindexlist,增加空列表、索引有效性校验:

private void TransferImagesBetweenFrames_Updatepbindexlist()
{          
    // 先检查bitmap_list是否为空
    if (bitmap_list.Count == 0)
    {
        MessageBox.Show("无可用图片数据");
        return;
    }

    foreach (var panel in panel_mid.Controls.OfType<Panel>().ToList())
    {
        foreach (var pb in panel.Controls.OfType<PictureBox>().ToList())
        {
            // 尝试解析PictureBox名称为整数,避免格式错误
            if (!int.TryParse(pb.Name, out int pbindex))
            {
                continue;
            }

            // 校验pbindex是否在dicomfileindexlist的有效范围内
            if (pbindex > 0 && pbindex <= dicomfileindexlist.Count)
            {
                int bitmapindex = dicomfileindexlist[pbindex - 1];
                // 校验bitmapindex是否在bitmap_list的有效范围内
                if (bitmapindex >= 0 && bitmapindex < bitmap_list.Count)
                {
                    // 清理旧图片资源
                    if (pb.Image != null)
                    {
                        pb.Image.Dispose();
                    }
                    pb.Image = bitmap_list[bitmapindex];
                    pbindexlist.Add(pbindex);
                }
                else
                {
                    Console.WriteLine($"无效的bitmap索引:{bitmapindex}");
                }
            }
        }
    }
}

额外注意事项

  • Bitmap资源管理:每次替换PictureBox的Image时,必须先Dispose旧的Bitmap,防止内存泄漏。
  • 类型安全解析:用int.TryParse替代Convert.ToInt16,避免PictureBox名称非整数时抛出异常。
  • 空值校验:对拖放数据、列表对象都要做空值/空状态检查,提升代码稳定性。

内容的提问来源于stack exchange,提问作者Mohan Tasare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 10:35:18