前言
一直没有稳定的DVS供我测试用,朋友那边也是频频宕掉,所以延误至今,所幸还是出来了。此外非常遗憾的是没能用WPF实践成功,关键是IntPtr句柄设置不对,没法显示出来,为了保证进度也只好暂时放弃用WPF做显示。
提醒
欢迎转载,但请保留博客园(www.cnblogs.com)、农民伯伯(over140.cnblogs.com)的出处,谢谢合作:)
系列
1. C# 海康DVR客户端开发系列(1)—— 准备
2. C# 海康DVR客户端开发系列(2)—— 封装API
3. 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();
}
#endregion
#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()));
return;
}
#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()));
// return;
//}
#endregion
}
/// <summary>
/// 停止预览并退出登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
HCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
#region New V30
HCNetSDK.NET_DVR_Logout_V30(lUserID);
#endregion
#region Old
//如果不退出登录,下次开启拾音将失效
//HCNetSDK.NET_DVR_Logout(lUserID);
#endregion
this.panel1.Invalidate(false);
}
#endregion
//登录标识
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();
}
#endregion
#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()));
return;
}
#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()));
// return;
//}
#endregion
}
/// <summary>
/// 停止预览并退出登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
HCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
#region New V30
HCNetSDK.NET_DVR_Logout_V30(lUserID);
#endregion
#region Old
//如果不退出登录,下次开启拾音将失效
//HCNetSDK.NET_DVR_Logout(lUserID);
#endregion
this.panel1.Invalidate(false);
}
#endregion
代码说明:
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