可以从int 数组中获取IntPtr吗?
问候。
在C#中:如果我有一个像这样声明的int []数组
int[] array = new array[size];
有一种方法可以从这个数组中获取IntPtr吗?
问题是我正在使用EmguCV框架,并且有一个构造函数来创建一个图像,它将IntPtr带到像素数据,以便从数组(int [])构建图像。
Image result = new Image(bitmap.Width,bitmap.Height,stride,"**array.toPointer??**");
顺便说一句,如果有人能告诉我如何计算步幅,这将是伟大的。
您应该能够使用GCHandle
没有不安全代码的情况下执行此GCHandle
。 这是一个示例:
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned); try { IntPtr pointer = handle.AddrOfPinnedObject(); } finally { if (handle.IsAllocated) { handle.Free(); } }
使用不安全的代码,如下所示:
unsafe { fixed (int* pArray = array) { IntPtr intPtr = new IntPtr((void *) pArray); } }