c# memcmp image compare error -
im trying compare 2 smalls blocks of image using memcmp
method. saw answer what fastest way can compare 2 equal-size bitmaps determine whether identical? , tried implement in project:
private void form1_load(object sender, eventargs e) { bitmap prev, curr; prev = (bitmap) image.fromfile(@"c:\users\public\desktop\b.png"); curr = (bitmap)image.fromfile(@"c:\users\public\desktop\b.png"); messagebox.show(comparememcmp(prev, curr).tostring()); }
this method-
[dllimport("msvcrt.dll")] private static extern int memcmp(intptr b1, intptr b2, long count); public static 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); try { intptr bd1scan0 = bd1.scan0; intptr bd2scan0 = bd2.scan0; int stride = bd1.stride; int len = stride * b1.height; return memcmp(bd1scan0, bd2scan0, len) == 0; } { b1.unlockbits(bd1); b2.unlockbits(bd2); } }
and im getting error when calling memcmp
function
a call pinvoke function 'windowsformsapplication1!windowsformsapplication1.form1::memcmp' has unbalanced stack. because managed pinvoke signature not match unmanaged target signature. check calling convention , parameters of pinvoke signature match target unmanaged signature.
any idea why happening? did based on answer.
the correct signature is:
[dllimport("msvcrt.dll", callingconvention=callingconvention.cdecl)] static extern int memcmp(intptr b1, intptr b2, intptr count);
because in c, count
size_t
, can 32 or 64 bits depending if program running @ 32 or 64 bits.
then use it:
return memcmp(bd1scan0, bd2scan0, (intptr)len) == 0;
Comments
Post a Comment