首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c# memcmp图像比较误差

c# memcmp图像比较误差
EN

Stack Overflow用户
提问于 2015-07-23 13:33:51
回答 1查看 641关注 0票数 1

我试着用memcmp方法比较两个小块的图像。我看到了这个答案-- What is the fastest way I can compare two equal-size bitmaps to determine whether they are identical?,我试图在我的项目中实现这一点:

代码语言:javascript
复制
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());
}

这就是方法-

代码语言:javascript
复制
[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;
    }
    finally
    {
        b1.UnlockBits(bd1);
        b2.UnlockBits(bd2);
    }
}

当调用memcmp函数时,我得到了这个错误

代码语言:javascript
复制
A call to PInvoke function 'WindowsFormsApplication1!WindowsFormsApplication1.Form1::memcmp' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

知道为什么会这样吗?我都是根据这个答案做的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-23 13:37:01

正确的签名是:

代码语言:javascript
复制
[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
static extern int memcmp(IntPtr b1, IntPtr b2, IntPtr count);

因为在C中,countsize_t,所以它可以是32位或64位,这取决于程序是否在32位或64位上运行。

然后使用它:

代码语言:javascript
复制
return memcmp(bd1scan0, bd2scan0, (IntPtr)len) == 0;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31588951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档