精簡版服務端本地預覽代碼:從 VC++ `OnPaint` 中提煉,僅保留單通道預覽邏輯。
// 成員變量
IntPtr _channelHandle;
private void Form2_Load(object sender, EventArgs e) {
HikVisionSDK.SetDefaultVideoStandard(VideoStandard_t.StandardNTSC);
if (HikVisionSDK.InitDSPs() < 0) { MessageBox.Show("DSP初始化失敗"); return; }
if (HikVisionSDK.GetTotalDSPs() == 0) { MessageBox.Show("無可用通道"); return; }
_channelHandle = HikVisionSDK.ChannelOpen(0);
HikVisionSDK.SetIBPMode(_channelHandle, 100, 2, 1, 25); // IBP 間隔、B幀數、P幀數、幀率
HikVisionSDK.SetDefaultQuant(_channelHandle, 15, 15, 20); // 量化參數
StartVideoPreview();
}
private void StartVideoPreview() {
Rectangle rect = panelVideo.ClientRectangle;
HikVisionSDK.StartVideoPreview(_channelHandle, panelVideo.Handle, ref rect, false,
(int)TypeVideoFormat.vdfRGB16, 25);
}
private void Form2_Move(object sender, EventArgs e) {
HikVisionSDK.StopVideoPreview(_channelHandle);
StartVideoPreview(); // 窗口移動需重繪
}
在預覽基礎上疊加服務端網絡回調,實現客戶端可連接(見下篇)。