본문 바로가기

C#

[Marshal] byte* to byte[]

        protected override void CamStreamStart()
        {
            uint i = 0;
            SNCameraStreamEvent evtarg = new SNCameraStreamEvent();
            while (true)
            {
                for (i = 0; i < cams.Length; ++i)
                {
                    cams[i].m_cameras.RetrieveBuffer(cams[i].m_rawImages);

                    int length = (int)(cams[i].m_rawImages.rows * cams[i].m_rawImages.stride);
                    unsafe
                    {
                        byte[] dstbytes = new byte[length];

                        // [ cams[i].m_rawImages.data ] is [ byte * ] 
                        Marshal.Copy((IntPtr)cams[i].m_rawImages.data, dstbytes, 0 , length);

                        cams[i].m_processedSrc = BitmapSource.Create(
                        (int)cams[i].m_rawImages.cols,
                        (int)cams[i].m_rawImages.rows,
                        96, 96,
                        System.Windows.Media.PixelFormats.Gray8,
                        null,
                        dstbytes,
                        (int)cams[i].m_rawImages.stride);
                        cams[i].m_processedSrc.Freeze();
                    }
                }

                if (CameraEvent != null)
                {
                    evtarg.state = STATE.UPDATE;
                    Application.Current.Dispatcher.Invoke((Action)delegate
                    {
                        CameraEvent(this, evtarg);
                    });
                }

                //Thread.Sleep(1000);
            }
        }

Marshal.Copy 주목.