我试图制作一个拍摄屏幕截图并进行比较的程序。
这是代码的示例
比较方法:
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int memcmp(IntPtr b1, IntPtr b2, UIntPtr count);
public bool CompareMemCmp(Bitmap b1, Bitmap b2)
{
if ((b1 == null) != (b2== null)) return false;
// if (b1.Size != b2.Size) return false;
var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bd2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
IntPtr bd1scan0 = bd1.Scan0;
IntPtr bd2scan0 = bd2.Scan0;
int stride = bd1.Stride;
int len = stride * b1.Height;
return memcmp(bd1scan0, bd2scan0, (UIntPtr)(len)) == 0;
}
这是主要代码:
private void MainForm_Load(object sender, EventArgs e)
{
prev = CaptureScreen.GetDesktopImage();
th = new Thread(new ThreadStart(capture));
th.Start();
}
private void capture()
{
while (true)
{
current = CaptureScreen.GetDesktopImage();
if (CompareMemCmp(prev, current))
{
label1.Invoke(new Action(() => label1.Text="changed"));
prev = current;
}
else
label1.Invoke(new Action(() => label1.Text = "same"));
count++;
}
}
我在该行的CompareMemCmp方法中得到一个奇怪的错误
var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
我猜这与截图相关...当我比较一个目录中的2张图片时它工作得很好...任何想法如何重新定位他们?