c#位图已锁定屏幕截图
我正在尝试制作一个程序,拍摄屏幕截图并进行比较 这是一个代码示例 比较方法:c#位图已锁定屏幕截图,c#,sockets,bitmap,screenshot,C#,Sockets,Bitmap,Screenshot,我正在尝试制作一个程序,拍摄屏幕截图并进行比较 这是一个代码示例 比较方法: [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] static extern int memcmp(IntPtr b1, IntPtr b2, UIntPtr count); public bool CompareMemCmp(Bitmap b1, Bitmap b2) { if
[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++;
}
}
我在那行的compareMecmp方法中遇到了一个奇怪的错误
var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
我想这和截图有关。。因为当我只比较一个目录中的两个图像时,效果很好。。。你知道如何让他们放松吗?在完成比较后,你应该在位图上调用
UnlockBits()
。请参见上的示例
您正在使用此库捕获它吗?最好给你的问题加上参考。这个错误有多奇怪?我看到你锁定了位图。在哪里解锁它们?如果((b1==null)!=(b2==null))返回false,还应更改行
因为如果两个引用都是null
,它将不会返回false。我想你是想写:if((b1==null)| |(b2==null))返回false代码>
...
int stride = bd1.Stride;
int len = stride * b1.Height;
b1.UnlockBits(bd1);
b2.UnlockBits(bd2);
return memcmp(bd1scan0, bd2scan0, (UIntPtr)(len)) == 0;
...