public static IntPtr StreamToIntPtr (Stream stream)
{
byte[] buffer = new byte[1024];
IntPtr buf = Marshal.AllocHGlobal ((int) stream.Length);
int ofs = 0;
int nread = 0;
if (stream.CanSeek && stream.Position != 0)
stream.Seek (0, SeekOrigin.Begin);
do {
nread = stream.Read (buffer, 0, 1024);
Marshal.Copy (buffer, 0, (IntPtr) (((long)buf)+ofs), nread);
ofs += nread;
} while (nread != 0);
return buf;
}