0

我对框架和我的应用程序的内存消耗有疑问。我制作了一个应用程序,它不时从我的网络摄像头拍摄快照,因为我的网络摄像头不支持快照功能,我必须创建一个新的帧事件并最终捕获并保存一帧。当前帧率为 5 FPS,内存消耗稳定,虽然约为 11MB。它足够好还是有什么方法可以减少内存使用量?

这是我的代码:

Imports AForge.Video
Imports AForge.Video.DirectShow
Imports System.Drawing.Imaging

Public Class Form1
    Dim VideoCaptureDevices As FilterInfoCollection
    Dim FinalVideo As VideoCaptureDevice
    Dim save As Boolean = False
    Dim Username As String = Environment.UserName
    Dim counter As Integer
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If FinalVideo.IsRunning Then
            FinalVideo.SignalToStop()
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Interval = 5000
        Timer1.Start()
        VideoCaptureDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
        If VideoCaptureDevices.Count Then
            FinalVideo = New VideoCaptureDevice(VideoCaptureDevices(0).MonikerString)
            AddHandler FinalVideo.NewFrame, AddressOf Me.video_NewFrame
            FinalVideo.Start()
            FinalVideo.DesiredFrameRate = 5
        Else
            MsgBox("No video input found")
        End If
    End Sub

    Private Sub video_NewFrame(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)

        Dim image As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
        Dim g As Graphics = Graphics.FromImage(image)
        If save Then
            counter += 1
            image.Save("C:\Users\" & Username & "\Desktop\" & counter & ".jpeg", ImageFormat.Jpeg)
            save = False
        End If
        g.Dispose()
        image.Dispose()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        save = True
    End Sub


End Class
4

0 回答 0