IO异常处理(参数无效)

时间:2013-12-01 07:15:24

标签: c#

这是我所知道的一段代码。它从目录中获取图像并与像素匹配。当程序从相同像素的目录中读取5个图像时,它完全没问题,但是当我们增加具有不同像素的图像或图像的数量时,它会引发异常“Argumentexcepton未处理”。请帮忙

string[] image_name = new string [1000] ;
check = d.GetFiles("*.jpg").Length;
while (imageArray < check)
        {
        double percentage = 0;
        count1 = 0;
        count2 = 0;
        progressBar1.Value++;
        //error occurs on this line
        img2 = new Bitmap(image_name[imageArray]);
        progressBar1.Maximum = check;
        if (img1.Width != img2.Width && img1.Height != img2.Height)
          {
            if (img2.Width > img1.Width || img2.Height > img1.Height)
                {
                img1 = ResizeBitmap(img1, img2.Width, img2.Height);
                }
            else
                img1 = ResizeBitmap(img1, img2.Width, img2.Height);
          }
            for (int i = 0; i < img1.Width; i++)
            {
                for (int j = 0; j < img1.Height; j++)
                {
                    img1_ref = img1.GetPixel(i, j).ToString();
                    img2_ref = img2.GetPixel(i, j).ToString();
                    if (img1_ref != img2_ref)
                    {
                    count2++;
                    break;
                    }
                    count1++;
                }
            }
            percentage = (count2 + count1);
            percentage = count1 / percentage;
            percentage = percentage * 100;
            if (percentage < 90)
              {
msg1 = "Sorry, Images are not same , " + count1 + " same pixels found and " +     count2 + " wrong pixels found";
    }
            else if (percentage == 100)
            {
                msg1 = " Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found";
               ResultImagePanel rmp = new ResultImagePanel(image_name[imageArray],         Convert.ToString(percentage) + "%", folderPath + "\\" + image_name[imageArray], msg1);
                rmp.Location = new Point(0, 345 * counter);
                panel1.Controls.Add(rmp);
            }
            else
            {
                msg1 = " Similar images , " + count1 + " same pixels found and " + count2 + " wrong pixels found";
                ResultImagePanel rmp = new ResultImagePanel(image_name[imageArray],  Convert.ToString(percentage) + "%", folderPath + "\\" + image_name[imageArray], msg1);
                rmp.Location = new Point(0, 345 * counter);
                panel1.Controls.Add(rmp);
            }
            counter++;
            imageArray++;
}
    }

2 个答案:

答案 0 :(得分:0)

执行该代码时,

image_name[imageArray]string.Empty。因此错误。在执行代码之前,您需要使用有效的文件名填写image_name数组。

答案 1 :(得分:0)

像这样实现你的数组image_name:

image_name = Directory.EnumerateFiles(path,“* .jpg”)。ToArray();