死宅单片机 2023-04-28 16:33 采纳率: 80%
浏览 50
已结题

C# Bitmap类出现参数无效异常,如何解决?

Bitmap处理完最后一张图片时,出现了ms(变量)参数无效,代码如下:

 private void button4_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == null) || (textBox2.Text == null) || (or == null) || (pu == null)) {
                MessageBox.Show("请完善信息!");
            }
            else
            {
                textBox1.ReadOnly = true;
                textBox2.ReadOnly = true;
                textBox3.ReadOnly = true;
                textBox4.ReadOnly = true;
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                
                string d1;
                string d2;
                List<string> na = new List<string>();
                DirectoryInfo d = new DirectoryInfo(or);
                FileInfo[] file = d.GetFiles();
                foreach (FileInfo f in file)
                {
                    na.Add(f.Name);
                }
                foreach(string filename in na) { 
                    i = 1;
                    a = 1;
                    FileInfo fpic = new FileInfo(or + @"\" + filename);
                    FileStream fs = new FileStream(or + @"\" + filename, FileMode.Open);
                    byte[] byDate = new byte[fs.Length];
                    fs.Read(byDate, 0, byDate.Length);
                    fs.Close();
                    MemoryStream ms = new MemoryStream(byDate);
                    Bitmap pic = new Bitmap(ms);
                    wi = pic.Width;
                    he = pic.Height;
                    listBox1.Items.Add(filename + "尺寸:" + wi.ToString() + "×" + he.ToString());
                    listBox1.TopIndex = listBox1.Items.Count - (int)(listBox1.Height / listBox1.ItemHeight);
                    do
                    {
                        if ((wi > (i - 1) * longth) && (wi <= i * longth))
                        {
                            d1 = ((i - 1) * longth).ToString() + "≤宽≤" + (i * longth).ToString();
                            if (!Directory.Exists(pu + @"\" + d1)) {
                                Directory.CreateDirectory(pu + @"\" + d1);
                            }
                            do
                            {
                                if ((he > (a - 1) * wideth) && (he <= a * wideth))
                                {
                                    d2 = ((a - 1) * wideth).ToString() + "≤高≤" + (a * wideth).ToString();
                                    if (!Directory.Exists(pu + @"\" + d1 + @"\" + d2))
                                    {
                                        Directory.CreateDirectory(pu + @"\" + d1 + @"\" + d2);
                                    }
                                    fpic.CopyTo(pu + @"\" + d1 + @"\" + d2 + @"\" + filename, true);
                                    ln = false;
                                } else
                                {
                                    ln = true;
                                    a++;
                                }
                            } while (ln);

                            en = false;
                        }
                        else
                        {
                            en = true;
                            i++;
                        }
                    } while (en);
                    pic.Dispose();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }

                listBox1.Items.Add("完成!");
                listBox1.TopIndex = listBox1.Items.Count - (int)(listBox1.Height / listBox1.ItemHeight);
                textBox1.ReadOnly = false;
                textBox2.ReadOnly = false;
                textBox3.ReadOnly = false;
                textBox4.ReadOnly = false;
                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = true;
            }
        
        }

异常如下:

img

问一下这怎么解决?

  • 写回答

3条回答 默认 最新

  • 於黾 2023-04-28 17:07
    关注

    你为啥要通过流转来转去的,直接写bitmap.fromfile不就行了
    此外,你在遍历文件夹,你确定文件夹里全是图片,没有乱七八糟别的东西吗,先判断类型再加入na,不要把非图片文件加入na

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月17日
  • 已采纳回答 6月9日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 grlb复位,设置设置返回行程为0,卡在home状态,怎么解决?
  • ¥100 CubeIDE更换芯片以及调整代码
  • ¥50 有没有可以远程指导问题
  • ¥15 origin柱状图,分组如何分
  • ¥15 两个不同IP互通的配置命令
  • ¥15 office弹窗激活问题
  • ¥15 明阳风电1.5s,风机重启后报出振动开关触发,无法复位求解。
  • ¥20 ankidroid软件兼容性问题
  • ¥15 请教合适的模型做回归
  • ¥15 stm32cubemx生成代码后利用printf输出浮点数卡死。