You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AForge.NET连接ONVIF IP摄像头时连接意外关闭的问题及ONVIF使用咨询

Fixing "Underlying Connection Closed" When Using AForge.NET with ONVIF Cameras

Great question—let's clear up a key point first: AForge.NET doesn't have native support for the ONVIF management protocol. The http://admin:admin@192.168.1.142/onvif/device_service URI you're using is for device configuration (like fetching device info or adjusting settings), not for pulling video streams. That mismatch is likely causing your connection error. Here's how to resolve this:

Step 1: Get the Correct Video Stream URI

Instead of using the ONVIF device service endpoint, you need your camera's RTSP or HTTP video stream address. You can find this in a few ways:

  • Check your camera's web admin panel (look for "Streaming" or "Media Settings" sections)
  • Use an ONVIF tool like ONVIF Device Manager to discover the camera and pull its media stream URIs
  • Common stream formats look like:
    • RTSP: rtsp://admin:admin@192.168.1.142:554/mainStream
    • HTTP MJPEG: http://admin:admin@192.168.1.142:8080/video

Step 2: Use AForge.NET to Play the Stream

AForge.NET is built for video capture and processing, so once you have the stream URI, you can use its video source classes to play it. Here are two common approaches:

Option 1: MJPEG Stream (if your camera supports it)

using AForge.Video;
using System.Drawing;

// Initialize the MJPEG stream with your camera's URI
var mjpegStream = new MJPEGStream("http://admin:admin@192.168.1.142:8080/video");

// Handle new frame events to display video
mjpegStream.NewFrame += (sender, eventArgs) =>
{
    // Clone the frame to avoid cross-thread issues
    using (var frame = (Bitmap)eventArgs.Frame.Clone())
    {
        // Update your UI element (e.g., PictureBox)
        pictureBox1.Image = frame;
    }
};

// Start the stream
mjpegStream.Start();

Option 2: RTSP Stream (use FFMPEG integration)

AForge's native RTSP support is limited, so you'll need the AForge.Video.FFMPEG package for better compatibility:

using AForge.Video.FFMPEG;

var videoSource = new VideoFileReader();
videoSource.Open("rtsp://admin:admin@192.168.1.142:554/mainStream");

// Read frames in a loop (or use a background thread)
while (videoSource.IsOpen)
{
    using (var frame = videoSource.ReadVideoFrame())
    {
        if (frame != null)
        {
            pictureBox1.Image = frame.Clone() as Bitmap;
        }
    }
}

Step 3: Troubleshoot Remaining Connection Issues

If you still get connection errors after using the correct stream URI, check these common issues:

  • Authentication: Double-check your username/password—some cameras require a separate stream account
  • Network: Ensure your computer and camera are on the same network segment; disable firewalls temporarily to rule out blocking
  • Protocol Settings: Some cameras force TCP for RTSP streams—add ?tcp to the end of the URI if needed (e.g., rtsp://.../mainStream?tcp)
  • AForge Version: Update to the latest version of AForge.NET to fix any outdated protocol support

Bonus: Use ONVIF to Automate Stream Discovery

If you want to programmatically get the stream URI via ONVIF (instead of hardcoding it), use a dedicated ONVIF library like ONVIF.NET or ONVIF Device Manager's API. You can call the GetProfiles ONVIF method to retrieve media stream URIs, then pass that URI to AForge.NET for playback.

内容的提问来源于stack exchange,提问作者ghasem deh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:14:01