sockets - c# Bitmap is already locked screenshots -
im trying make program takes screen shots , compare them.
this sample of code
compare method:
[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; }
thisis main code:
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++; } }
im getting weird error int comparememcmp method @ line
var bd1 = b1.lockbits(new rectangle(new point(0, 0), b1.size), imagelockmode.readonly, pixelformat.format32bppargb);
i guess it's related screenshot take.. cuz when compare 2 images directory works fine... idea how relase them guys?
you should call unlockbits()
on bitmap after done comparing. see example on msdn.
... int stride = bd1.stride; int len = stride * b1.height; b1.unlockbits(bd1); b2.unlockbits(bd2); return memcmp(bd1scan0, bd2scan0, (uintptr)(len)) == 0; ...
Comments
Post a Comment