1

メソッドを使用して、画像の2つの小さなブロックを比較しようとしていmemcmpます。この回答を見ました2 つの等しいサイズのビットマップを比較して同一かどうかを判断する最速の方法 そして私は私のプロジェクトでこれを実装しようとしました:

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());
}

これが方法です-

[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関数を呼び出すときにこのエラーが発生します

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.

なぜそれが起こっているのですか?私はその答えに基づいてすべてをしました。

4

1 に答える 1

2

正しい署名は次のとおりです。

[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
static extern int memcmp(IntPtr b1, IntPtr b2, IntPtr count);

C ではcountsize_tであるため、プログラムが 32 ビットで実行されているか 64 ビットで実行されているかに応じて、32 ビットまたは 64 ビットになる可能性があります。

それを使用するには:

return memcmp(bd1scan0, bd2scan0, (IntPtr)len) == 0;
于 2015-07-23T13:37:01.207 に答える