天天看點

C# 海康DVR用戶端開發系列(3)—— 連接配接DVR和圖像預覽

前言

  一直沒有穩定的dvs供我測試用,朋友那邊也是頻頻宕掉,是以延誤至今,所幸還是出來了。此外非常遺憾的是沒能用wpf實踐成功,關鍵是intptr句柄設定不對,沒法顯示出來,為了保證進度也隻好暫時放棄用wpf做顯示。

提醒

系列

正文

  一、截圖

   

C# 海康DVR用戶端開發系列(3)—— 連接配接DVR和圖像預覽

  二、代碼

C# 海康DVR用戶端開發系列(3)—— 連接配接DVR和圖像預覽

        #region member variable

        //登入辨別

        private int luserid = -1;

        //預覽辨別

        private int lrealhandle = -1;

        #endregion

        #region form

        public frmmain()

        {

            initializecomponent();

        }

        private void frmmain_load(object sender, eventargs e)

            hcnetsdk.net_dvr_init();

            hcnetsdk.net_dvr_setconnecttime(5000, 1);

        private void frmmain_formclosing(object sender, formclosingeventargs e)

            hcnetsdk.net_dvr_cleanup();

        #region 功能

        /// <summary>

        /// 登入dvs并預覽

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnopen_click(object sender, eventargs e)

            #region new v30

            net_dvr_deviceinfo_v30 dev = new net_dvr_deviceinfo_v30();

            luserid = hcnetsdk.net_dvr_login_v30("192.168.1.1", 8000, "admin", "12345", out dev);

            if (luserid == -1)

            {

                messagebox.show(string.format("登入dvs失敗,錯誤碼: {0}", hcnetsdk.net_dvr_getlasterror()), "登入dvs失敗", messageboxbuttons.ok, messageboxicon.error);

                return;

            }

            net_dvr_clientinfo clientinfo = new net_dvr_clientinfo();

            clientinfo.hplaywnd = this.panel1.handle;//視訊視窗

            clientinfo.lchannel = 2; // 通道号

            clientinfo.llinkmode = 0;

            clientinfo.smulticastip = "234.5.6.7";

            lrealhandle = hcnetsdk.net_dvr_realplay_v30(luserid, ref clientinfo, null, 1, false);

            if (lrealhandle == -1)

                messagebox.show(string.format("播放失敗,錯誤碼: {0}", hcnetsdk.net_dvr_getlasterror()));

            #endregion

            #region old

            //net_dvr_deviceinfo dev = new net_dvr_deviceinfo();

            //luserid = hcnetsdk.net_dvr_login("125.119.30.175", 8000, "admin", "12345", out dev);

            //if (luserid == -1)

            //{

            //    messagebox.show(string.format("登入dvs失敗,錯誤碼: {0}", hcnetsdk.net_dvr_getlasterror()), "登入dvs失敗", messageboxbuttons.ok, messageboxicon.error);

            //    return;

            //}

            //net_dvr_clientinfo clientinfo = new net_dvr_clientinfo();

            //clientinfo.hplaywnd = this.panel1.handle;//視訊視窗

            //clientinfo.lchannel = 2; // 通道号

            //clientinfo.llinkmode = 0;

            //clientinfo.smulticastip = "234.5.6.7";

            //lrealhandle = hcnetsdk.net_dvr_realplay(luserid, ref clientinfo);

            //if (lrealhandle == -1)

            //    messagebox.show(string.format("播放失敗,錯誤碼: {0}", hcnetsdk.net_dvr_getlasterror()));

        /// 停止預覽并登出

        private void btnclose_click(object sender, eventargs e)

            hcnetsdk.net_dvr_stoprealplay(lrealhandle);

            hcnetsdk.net_dvr_logout_v30(luserid);

            //如果不登出,下次開啟拾音将失效

            //hcnetsdk.net_dvr_logout(luserid);

            this.panel1.invalidate(false);

C# 海康DVR用戶端開發系列(3)—— 連接配接DVR和圖像預覽

    代碼說明:

      1.  [#region new]與[#region old]代碼塊分别是新舊版本的api,這裡均測試通過。

      2.  需要把hcnetsdk.dll(這裡事例使用版本為3.0.3.3)直接拷貝到bin\debug下去,或者拷貝到項目工程根目錄下,然後選中右鍵屬性->複制到輸出目錄 選中始終複活或者如果較新則複制,重新編譯即可。

  三、提醒

    提醒大家api有變動,請立即更新!

  五、後期維護 

    1.  2009-11-2

      也需要把playctrl.dll加到的項目中來,否則可能出現登入成功,播放報錯,顯示錯誤代碼64。

結束語

困難重重,堅持做伴。

轉自:http://www.cnblogs.com/over140/archive/2009/10/31/1588343.html