E 'possibile ottenere un IntPtr da un [] array int?
Domanda
Saluti.
In C #: Se ho un [] array int dichiarato come questo
int[] array = new array[size];
c'è un modo per ottenere l'IntPtr da questo array?
Il fatto è che sto utilizzando il framework EmguCV, e v'è una costruzione per creare un'immagine che prende un IntPtr ai dati dei pixel, al fine di costruire un'immagine da un array (int []).
Image<Gray,Int32> result = new Image<Gray,int>(bitmap.Width,bitmap.Height,stride,"**array.toPointer??**");
A proposito se qualcuno potesse detto come calcolare il passo, che sarebbe grande.
Soluzione
Si dovrebbe essere in grado di fare questo senza codice unsafe utilizzando GCHandle
. Ecco un esempio:
GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
try
{
IntPtr pointer = handle.AddrOfPinnedObject();
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
}
Altri suggerimenti
Usa il codice non sicuro, in questo modo:
unsafe
{
fixed (int* pArray = array)
{
IntPtr intPtr = new IntPtr((void *) pArray);
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow