int [] 배열에서 intptr을 얻을 수 있습니까?
문제
인사말.
C#: int [] 배열이 다음과 같이 선언 된 경우
int[] array = new array[size];
이 배열에서 intptr을 얻는 방법이 있습니까?
문제는 EMGUCV 프레임 워크를 사용하고 있으며 배열 (int [])에서 이미지를 빌드하기 위해 intptr을 픽셀 데이터로 가져 오는 이미지를 만들 수있는 생성자가 있다는 것입니다.
Image<Gray,Int32> result = new Image<Gray,int>(bitmap.Width,bitmap.Height,stride,"**array.toPointer??**");
그건 그렇고 누군가가 보폭을 계산하는 방법을 말해 줄 수 있다면 그것은 좋을 것입니다.
해결책
안전하지 않은 코드를 사용하지 않고이 작업을 수행 할 수 있어야합니다. 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);
}
}
제휴하지 않습니다 StackOverflow