【问题标题】:MJPEGStream Function Example of Aforge.Video LibraryAforge.Video 库的 MJPEGStream 函数示例
【发布时间】:2012-05-31 14:39:33
【问题描述】:

有人可以给我一个完整的例子,使用这个用 VB.NET 或 C# 编写的库在图片框中显示流的图像

【问题讨论】:

  • 我想从 IPCAM 获取图像,这个 cam 有登录验证

标签: c# windows vb.net mjpeg aforge


【解决方案1】:

这是一个使用 AForge.Video.MJPEGStream 类的非常非常简单的表单。

private MJPEGStream VideoStream = new MJPEGStream();

private void frmMain_Load(object sender, System.EventArgs e)
{
    VideoStream.Source = "URL_HERE";
    VideoStream.Login = "USERNAME_HERE";
    VideoStream.Password = "PASSWORD_HERE";

    VideoStream.Start();
}

private void frmMain_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
    VideoStream.Stop();
}

private void VideoStream_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    Bitmap FrameData = new Bitmap(eventArgs.Frame);

    pbStream.Image = FrameData;
}

private void VideoStream_VideoSourceError(object sender, AForge.Video.VideoSourceErrorEventArgs eventArgs)
{
    Debug.WriteLine(eventArgs.Description);
}
public frmMain()
{
    InitializeComponent();
    this.FormClosing += new EventHandler(frmMain_FormClosing);
    this.Load += new EventHandler(frmMain_Load);
    VideoStream.NewFrame += new EventHandler(VideoStream_NewFrame);
    VideoStream.VideoSourceError+= new EventHandler(VideoStream_VideoSourceError);
}

【讨论】:

  • 感谢您的回答,但我不知道为什么我的图片框没有刷新图像,请看goo.gl/UuqnV
  • 在设置图片框图像后查看是否添加 pbStream.Refresh() 修复它。
  • 另外,在我的示例中,我向您展示了如何订阅该事件。我已经更新了这个例子。在 frmMain() 下查看您应该会注意到新的事件订阅。
  • 嗨,我使用这个网址:VideoStream.Source = "192.168.0.120/video/mjpg.cgi"; VideoStream.Login = "管理员"; VideoStream.Password = "管理员"; VideoStream.Start();但发送 404 错误 -> remute server not found
  • 不错的解决方案。它可以读取 RTSP 吗? url="rtsp://192.168.1.168:80/ch0_0.264" 错误:无法识别URI前缀
【解决方案2】:

另一个例子:

    Imports AForge.Video

Public Class Form1
    Inherits Form
    Private stopWatch As Stopwatch = Nothing

    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs)
        CloseCurrentVideoSource()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ' create video source
        Dim mjpegSource As New MJPEGStream("http://190.6.206.170/video/mjpg.cgi")
        mjpegSource.Login = "admin"
        mjpegSource.Password = ""
        ' open it
        OpenVideoSource(mjpegSource)

    End Sub

    ' Open video source
    Private Sub OpenVideoSource(source As IVideoSource)
        ' set busy cursor
        Me.Cursor = Cursors.WaitCursor

        ' stop current video source
        CloseCurrentVideoSource()

        ' start new video source
        videoSourcePlayer.VideoSource = source
        videoSourcePlayer.Start()

        ' reset stop watch
        stopWatch = Nothing

        ' start timer
        timer.Start()

        Me.Cursor = Cursors.[Default]
    End Sub

    ' Close video source if it is running
    Private Sub CloseCurrentVideoSource()
        If videoSourcePlayer.VideoSource IsNot Nothing Then
            videoSourcePlayer.SignalToStop()

            ' wait ~ 3 seconds
            For i As Integer = 0 To 29
                If Not videoSourcePlayer.IsRunning Then
                    Exit For
                End If
                System.Threading.Thread.Sleep(100)
            Next

            If videoSourcePlayer.IsRunning Then
                videoSourcePlayer.[Stop]()
            End If

            videoSourcePlayer.VideoSource = Nothing
        End If
    End Sub

    ' New frame received by the player
    Private Sub videoSourcePlayer_NewFrame(sender As Object, ByRef image As Bitmap)
        Dim now As DateTime = DateTime.Now
        Dim g As Graphics = Graphics.FromImage(image)

        ' paint current time
        Dim brush As New SolidBrush(Color.Red)
        g.DrawString(now.ToString(), Me.Font, brush, New PointF(5, 5))
        brush.Dispose()

        g.Dispose()
    End Sub


    Private Sub timer_Tick(sender As System.Object, e As System.EventArgs) Handles timer.Tick
        Dim videoSource As IVideoSource = videoSourcePlayer.VideoSource

        If videoSource IsNot Nothing Then
            ' get number of frames since the last timer tick
            Dim framesReceived As Integer = videoSource.FramesReceived

            If stopWatch Is Nothing Then
                stopWatch = New Stopwatch()
                stopWatch.Start()
            Else
                stopWatch.[Stop]()

                Dim fps As Single = 1000.0F * framesReceived / stopWatch.ElapsedMilliseconds
                Me.Text = fps.ToString("F2") & " fps"
                stopWatch.Reset()
                stopWatch.Start()
            End If
        End If
    End Sub
End Class

必需的控制:

System.Windows.Forms.Timer 'For display fps
Aforge.Controls.VideoSourcePlayer 'For Display the video Img

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2014-08-22
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多