I don't believe there is a framework method that will work. Marshal.Copy can copy memory into and out of an IntPtr
buffer, but it doesn't work with pointers.
Instead you can P/Invoke the native MoveMemory function which copies memory between two pointers.
[DllImport("Kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=false)]
static extern void MoveMemory(IntPtr dest, IntPtr src, UIntPtr size);
...
byte* imgData; // image data
uint uiDataSize; // image size
...
IntPtr ptr;
buffer.GetPointer(out ptr);
MoveMemory(ptr, (IntPtr)imgData, (UIntPtr)uiDataSize);
Yes the size
parameter of MoveMemory
is UIntPtr
not an int
because the SIZE_T
used in the native code is 32 bits on 32 bit systems and 64 bits on 64 bit systems.