work24 通过本文主要向大家介绍了c#获取ip,c#tcp/ip,c#获取本机ip,c#获取本机ip地址,c#tcp/ip通讯等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文实例讲述了C#实现IP摄像头的方法。分享给大家供大家参考。具体实现方法如下:
#region IP摄像头代码 /// <summary> /// ip摄像头代码 /// </summary> //视频 private IntPtr m_hChannel; private IntPtr m_hServer, phPlay; public delegate void MyInvoke(string str); public delegate void OutDelegate(IntPtr hSearch, DVSNET_FILE_TIME pFileTime, DVSNET_FILE_INFO pFileInfo, IntPtr pcontext); //远程视频处理 public void BeforeRecordFrame(IntPtr hChannel, ref RECORD_INFO pRecordInfo, ref FRAME_HEADER pFrameHeader, IntPtr pcontext) { } DVSNETClient.FileSearchCallback File_SearchCallback; //远程视频回放 public void FileSearchCallback(IntPtr hSearch, ref DVSNET_FILE_TIME pFileTime, ref DVSNET_FILE_INFO pFileInfo, IntPtr pcontext) { OutText(hSearch, pFileTime, pFileInfo, pcontext); } public void OutText(IntPtr hSearch, DVSNET_FILE_TIME pFileTime, DVSNET_FILE_INFO pFileInfo, IntPtr pcontext) { if (lvVideo.InvokeRequired) { OutDelegate outdelegate = new OutDelegate(OutText); this.BeginInvoke(outdelegate, new object[] { hSearch, pFileTime, pFileInfo, pcontext }); return; } lvVideo.View = View.Details; lvVideo.FullRowSelect = true; for (int i = 0; i < pFileInfo.sFileName.Count(); i++) { ListViewItem item1 = new ListViewItem(pFileInfo.sFileName, i); lvVideo.Items.AddRange(new ListViewItem[] { item1 }); } } private void pb_del_video_Click(object sender, EventArgs e) { this.Close(); } public void SearchNotify(int nDevType,string sServName,string sServSerial,string sUrl,IntPtr MacAddr,ushort wWebPort,ushort wDataPort,string sSubNetMask,string sGateWay,string sMultiIp,string sDNS,ushort wMultiPort,int nChCount,uint bDHCP,IntPtr pcontext) { string str = Convert.ToString(wDataPort); //string str2 = sUrl + "(" + str + ")"; string str2 = sUrl; this.AddText(str2); } //赋值给cmbIP private void AddText(string str) { MyInvoke method = new MyInvoke(this.AddTextToCmb); base.BeginInvoke(method, new object[] { str }); } public void AddTextToCmb(string str) { this.CmbIP.Items.Add(str); } //s搜索服务器 private void btnVideoSearck_Click(object sender, EventArgs e) { this.CmbIP.Items.Clear(); errornum errornum = DVSNETClient.DVSNET_SearchServers(0xbb8, 1, new DVSNETClient.SearchNotify(this.SearchNotify), base.Handle, IntPtr.Zero); if (errornum == errornum.DVSNET_ERR_OK) { timerVideo.Enabled = true; btnVideoSearck.Enabled = false; } else { } } //打开链接 private void btnVideoOpen_Click(object sender, EventArgs e) { string text = this.CmbIP.Text; string sServIp = text; int index = text.IndexOf('('); if (index != -1) { string str3 = new string(text.ToCharArray(), 0, index); sServIp = str3; } errornum errornum = DVSNETClient.DVSNET_OpenServer(this.EdtName.Text, sServIp, Convert.ToUInt16(this.EdtPort.Text), this.EdtUser.Text, this.EdtPassword.Text, ref this.m_hServer, 0, 0); this.btnVideoOpen.Enabled = false; //不能点击 if (errornum == errornum.DVSNET_ERR_OK) { DVSNET_SERVER_INFO dvsnet_server_info = new DVSNET_SERVER_INFO(); MessageBox.Show("打开链接成功!"); dvsnet_server_info = new DVSNET_SERVER_INFO { lStructSize = Marshal.SizeOf(dvsnet_server_info) }; DVSNETClient.DVSNET_GetServerInfo(this.m_hServer, ref dvsnet_server_info); // this.CmbChannel.Items.Clear(); for (int i = 0; i < dvsnet_server_info.nChnNum; i++) { int num3 = i + 1; // this.CmbChannel.Items.Add(num3.ToString()); } // this.CmbChannel.SelectedIndex = 0; } else { MessageBox.Show("打开链接失败,错误码为:" + errornum.ToString()); this.btnVideoOpen.Enabled = true; //不能点击 } } //关闭链接 private void btnVideoClose_Click(object sender, EventArgs e) { if (this.m_hChannel != IntPtr.Zero) { MessageBox.Show("请关闭通道!再关闭链接"); } else { if (this.m_hServer != IntPtr.Zero) { DVSNETClient.DVSNET_CloseServer(this.m_hServer); this.m_hServer = IntPtr.Zero; this.btnVideoOpen.Enabled = true; //不能点击 } } } //打开通道 IntPtr selServer = IntPtr.Zero; private void btnVideoOpenChnnel_Click(object sender, EventArgs e) { this.panelVideo.Visible = true; this.pannelVideoTwo.Visible = false; if (this.m_hServer != IntPtr.Zero) { this.btnVideoOpenChnnel.Enabled = false; //不能点击 DVSNET_CHANNEL_INFO dvsnet_channel_info = new DVSNET_CHANNEL_INFO(); MessageBox.Show("打开通道成功!请稍候"); dvsnet_channel_info = new DVSNET_CHANNEL_INFO { lStructSize = Marshal.SizeOf(dvsnet_channel_info), nProtocol = 0, hWndDisplay = this.panelVideo.Handle, bPlayStart = 1, dwStreamNo = 0 }; DVSNETClient.DVSNET_OpenChannel(this.m_hServer, Convert.ToUInt16(0), ref dvsnet_channel_info, ref this.m_hChannel); } else { MessageBox.Show("请打开链接!"); } } //关闭通道 private void btnVideoCloseChnnel_Click(object sender, EventArgs e) { if (this.m_hChannel != IntPtr.Zero) { DVSNETClient.DVSNET_CloseChannel(this.m_hChannel); MessageBox.Show("关闭通道成功!请稍候"); this.m_hChannel = IntPtr.Zero; this.btnVideoOpenChnnel.Enabled = true; //不能点击 } } //开始录像 private void btnVideoOn_Click(object sender, EventArgs e) { try { string ss = DateTime.Now.ToString().Replace(":", "-").Replace(" ", "-").Replace("/", "-") + ".asf"; if (this.panelVideo.Visible == false) { MessageBox.Show("请切换到打开通道"); } else { if (this.m_hChannel != IntPtr.Zero) { this.btnVideoOn.Enabled = false; //不能点击 //DVSNETClient.DVSNET_StartRecord(this.m_hChannel, 1, FILETYPE.FILETYPE_ASF, ss, new DVSNETClient.BeforeRecordFrame(this.BeforeRecordFrame), base.Handle); DVSNETClient.DVSNET_StartRecord(this.m_hChannel, 1, FILETYPE.FILETYPE_ASF, ss,Before_RecordFrame, base.Handle); } else { MessageBox.Show("请打开通道!"); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } //停止录像 private void btnStopRec_Click(object sender, EventArgs e) { DVSNETClient.DVSNET_StopRecord(this.m_hChannel); MessageBox.Show("已停止录像!"); this.btnVideoOn.Enabled = true; //不能点击 } //打开本地视频 IntPtr phPlayer; private void btnsearch_Click(object sender, EventArgs e) { try { if (m_hServer != IntPtr.Zero) { string fName = ""; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Application.StartupPath; //注意这里写路径时要用c:\而不是c: openFileDialog.Filter = "文本文件|*.asf|所有文件|*.asf"; openFileDialog.RestoreDirectory = true; openFileDialog.FilterIndex = 1; if (openFileDialog.ShowDialog() == DialogResult.OK) { this.panelVideo.Visible = false; this.pannelVideoTwo.Visible = true; this.btnsearch.Enabled = false; fName = openFileDialog.FileName; IntPtr hDisplayWnd = pannelVideoTwo.Handle; phPlayer = (IntPtr)0; DVSNETClient.DVSPLAYER_ReleaseInstance(phPlayer); //释放 DVSNETClient.DVSPLAYER_Stop(phPlayer); //释放本地 errornum aa = DVSNETClient.DVSPLAYER_CreateInstance(hDisplayWnd, ref phPlayer, 0); aa = DVSNETClient.DVSPLAYER_OpenFile(phPlayer,ref fName, 1); DVSNETClient.DVSPLAYER_Start(phPlayer); } else { this.panelVideo.Visible = true; this.pannelVideoTwo.Visible = false; } } else { MessageBox.Show("请打开通道!"); } } catch (Exception) { throw; } } //停止视频播放 private void btnhf_Click(object sender, EventArgs e) { this.panelVideo.Visible = true; this.pannelVideoTwo.Visible = false; IntPtr phPlayer = (IntPtr)0; DVSNETClient.DVSPLAYER_Stop(phPlayer); Messa