string pathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorted\";

var files = Directory.GetFiles(pathFile).Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension)).Where(name => { int number; return int.TryParse(name, out number); }).Select(name => int.Parse(name)).OrderBy(number => number).ToArray();
List<int> fileList = files.ToList();

image1 = new Bitmap(pathFile + fileList[0].ToString() + ".png");
image2 = new Bitmap(pathFile + fileList[1].ToString() + ".png");

if (compare(image2, image2))
{
    // if it's equal
    File.Delete(image2.ToString());
}

So basically what I have right now is that every file is numeric (without extension). I created an array then converted it to a list.

I use my global variables image1 and image2 which are used to compare if they are the same.

image1 and image2 change as we go along the search. So it changes the index.

If my compare() method returns a true, it will delete the second image.

However, on the compare() method, I seem to be getting this exception error on this line:

BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat);
有帮助吗?

解决方案

Well, actually it took me quite a while to notice this line:

if (compare(image2, image2))

You are passing the same Bitmap to the function compare, so when the function attempts to call LockBits on the same Bitmap twice, this exception occurs.

So in order to fix it, don't pass the same bitmaps to the function.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top