日期:2014-05-17  浏览次数:21054 次

怎么将内存中的图像数据读出来?求高手!!!
C#中,调用的C++ DLL,在DLL中有API函数:

LONG APIENTRY devwdm_GetImageBuffer(BYTE *pImageMem);

函数功能:      采集一帧RGB24图像到内存
pImageMem:     图像缓冲区指针

C#调用:

        [DllImport("devwdm.dll")]
        //函数功能:采集一帧RGB24图像到内存   pImageMem:图像缓冲区指针
        public static extern int devwdm_GetImageBuffer(IntPtr pImageMem);

请问我怎么才能将该函数存入到内存中的图片数据读出来,并用Panel控件在Form中显示出来啊?


------解决方案--------------------
//要读取的内存长度
int len=100;
byte[] imgByte=new byte[len];
IntPtr hglobal = Marshal.AllocHGlobal(len);
devwdm_GetImageBuffer(hglobal);
pictureBox1.Image = Image.FromStream(new MemoneryStream(imgByte));//imgByte为内存数据
Marshal.FreeHGlobal(hglobal);


就是先申请一块内存区域,然后再存放你要读取的内存中的数据。
------解决方案--------------------
int len=1024*1024*1024;//尽可能大,超过图像数据的长度;
IntPtr hglobal = Marshal.AllocHGlobal(len);
len=devwdm_GetImageBuffer(hglobal);
byte[] imgByte=new byte[len];
Marshal.Copy(hglobal,imgByte,0,len);
Marshal.FreeHGlobal(hglobal);
pictureBox1.Image = Image.FromStream(new MemoneryStream(imgByte));