Bitmap region is already locked

南笙酒味 提交于 2020-01-25 12:02:50

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/21346211/bitmap-region-is-already-locked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!