gpt4 book ai didi

C#位图比较(PinvokeStackimbalance异常)

转载 作者:行者123 更新时间:2023-11-30 14:37:21 27 4
gpt4 key购买 nike

我正在尝试使用以下方法比较两张图片:

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

我正在像这样使用 CompareMemCmp()(on_click 事件):

        Bitmap img1 = new Bitmap(@"C:\1\1.png");
Bitmap img2 = new Bitmap(@"C:\1\2.png");

if (CompareMemCmp(img1, img2) == true)
{ textBox1.Text = "Same"; }
else { textBox1.Text = "Different"; }

不幸的是抛出了一个异常:

return memcmp(bd1scan0, bd2scan0, len) == 0;

PinvokeStackimbalance“对 PInvoke 函数 'TextRecognition!TextRecognition.Form1::memcmp' 的调用使堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名。”

可能是什么问题?我已经尝试过不同的方法来解决这个问题..

最佳答案

pinvoke.net says the signature should be

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

编辑:pinvoke.net 已将声明的原始版本标记为仅 x64,但它似乎在 x32 上运行良好,只需添加 CallingConvention=CallingConvention.Cdecl

关于C#位图比较(PinvokeStackimbalance异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9348946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com