我导出了以下要在 DLL 文件中调用的 C 函数。
uint8_t* _stdcall GetCurrentImage();
现在我想在 C# 中调用此函数来获取位图图像。 我该怎么做?
提前致谢!
最佳答案
您需要知道返回的确切字节数和位图尺寸(高度、宽度和编码)。然后您可以在 C# 中将其声明为:
[DllImport("yourlib.dll")]
private static extern IntPtr GetCurrentImage();
您从中获得的 IntPtr 可以与 Marshal.Copy
一起使用, 获取原始字节:
byte[] buffer = new byte[length];
IntPtr beginPtr = GetCurrentImage();
Marshal.Copy(beginPtr, buffer,0,length);
最后声明一个Bitmap与图像的尺寸和 PixelFormat使用(如果它是非标准像素格式,您可能需要自己进行一些转换)。然后您可以使用 LockBits 将数据复制到原始位图字节中得到 BitmapData指向原始位图数据的实例。
关于c# - 将 C uint8_t 指针转换为 C# 字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4064024/