简体   繁体   中英

PictureBox.Handle Intptr to Image or Bitmap

I need help with HikVision IPCam Video streaming. I searched for 2 hole weeks without luck.

My problem is the IPCamm DLL stream the image into a picturebox using PictureBox.Handle. Its working perfectly fine:

[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_RealPlay_V30(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo, RealDataCallBack_V30 fRealDataCallBack_V30, IntPtr pUser, bool bBlocked);

    this.realDataCallBack = new RealDataCallBack_V30(RealDataCallback);
    this.clientInfo.hPlayWnd = PictureBox.Handle;
    this.clientInfo.lChannel = channel;
    this.clientInfo.lLinkMode = 0;

    this.playHandle = NET_DVR_RealPlay_V30(this.userID, ref this.clientInfo, realDataCallBack, IntPtr.Zero, true);

My Issue is that I need to process the image but I couldn't have any way to capture the image as Bitmap or Image and then display it As I like.

I tried Bitmap.FromHbitmap(PictureBox.Handle), Tried some MemoryMarshel solutions with no luck.

My Only way to get it now is by getting the data from call back functions which is with lower quality, lower frame-count, ...

This snippet draws the data from the handle into a bitmap and then sets the image of the picturebox. The CopyFromScreen line might not be necessary on older systems.

PictureBox.Image = CaptureControl(PictureBox.Handle, PictureBox.Width, PictureBox.Height);
// PictureBox.Image now contains the data that was drawn to it

    [DllImport("gdi32.dll")]
    private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    public Bitmap CaptureControl(IntPtr handle, int width, int height)
    {
      Bitmap controlBmp;
      using (Graphics g1 = Graphics.FromHwnd(handle))
      {
        controlBmp = new Bitmap(width, height, g1);
        using (Graphics g2 = Graphics.FromImage(controlBmp))
        {
         g2.CopyFromScreen(this.Location.X + PictureBox.Left, this.Location.Y + PictureBox.Top, 0, 0, PictureBox.Size);

          IntPtr dc1 = g1.GetHdc();
          IntPtr dc2 = g2.GetHdc();

          BitBlt(dc2, 0, 0, width, height, handle, 0, 0, 13369376);
          g1.ReleaseHdc(dc1);
          g2.ReleaseHdc(dc2);
        }
      }

      return controlBmp;
    }

You need to set hPlayWnd as zero. Set callback function to work on decoded data. I try to understand Hikvision SDK, a few difficult...

lpPreviewInfo.hPlayWnd = IntPtr.Zero;//预览窗口 live view window
m_ptrRealHandle = RealPlayWnd.Handle;
RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);//预览实时流回调函数 real-time stream callback function 
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, RealData, pUser);

public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr pUser)



//解码回调函数
        private void DecCallbackFUN(int nPort, IntPtr pBuf, int nSize, ref PlayCtrl.FRAME_INFO pFrameInfo, int nReserved1, int nReserved2)
        {
            // 将pBuf解码后视频输入写入文件中(解码后YUV数据量极大,尤其是高清码流,不建议在回调函数中处理)
            if (pFrameInfo.nType == 3) //#define T_YV12 3
            {
            //    FileStream fs = null;
            //    BinaryWriter bw = null;
            //    try
            //    {
            //        fs = new FileStream("DecodedVideo.yuv", FileMode.Append);
            //        bw = new BinaryWriter(fs);
            //        byte[] byteBuf = new byte[nSize];
            //        Marshal.Copy(pBuf, byteBuf, 0, nSize);
            //        bw.Write(byteBuf);
            //        bw.Flush();
            //    }
            //    catch (System.Exception ex)
            //    {
            //        MessageBox.Show(ex.ToString());
            //    }
            //    finally
            //    {
            //        bw.Close();
            //        fs.Close();
            //    }
            }
        }

See the source code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM