PictureBox.Handle Intptr到图像或位图

时间:2015-03-16 12:40:12

标签: c# bitmap picturebox handle ip-camera

我需要HikVision IPCam视频流的帮助。我没有运气搜索了2个星期。

我的问题是IPCamm DLL使用PictureBox.Handle将图像流映射到图片框中。它的工作非常好:

[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);

我的问题是我需要处理图像,但我无法将图像捕获为位图或图像,然后根据需要显示它。

我尝试了Bitmap.FromHbitmap(PictureBox.Handle),尝试了一些没有运气的MemoryMarshel解决方案。

我现在获得它的唯一方法是从回调函数中获取数据质量较低,帧数较少的数据......

2 个答案:

答案 0 :(得分:0)

此代码段将来自手柄的数据绘制到位图中,然后设置图片框的图像。在较旧的系统上,可能不需要CopyFromScreen行。

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;
    }

答案 1 :(得分:0)

您需要将 hPlayWnd 设置为零。设置回调函数以处理解码数据。尝试了解海康威视SDK,有点难...

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();
            //    }
            }
        }

source code