Click here to Skip to main content
14,355,411 members

Take Photos from a Webcam or IP Camera (SmartPhone using an App) with aforge.net

Rate this:
3.00 (3 votes)
Please Sign up or sign in to vote.
3.00 (3 votes)
25 Jun 2019MIT
Take photos from a webcam or IP Camera in VB.NET

Introduction

This article shows in a very simple way how to take photos from a webcam or with an Android phone as IP Camera using the Aforge.net library, in Visual Basic .NET.

Background

I needed to take pictures for ID cards using a webcam, however, on a machine we were using, we could not install the webcam and the idea of placing an IP camera. Doing a little research, I discovered that you could use an Android phone with a APP as an IP camera, so I decided to make the same program capture both from a webcam and an IP camera, the Aforge.video library made things much easier.

Another specific need is that the photo had to have a certain size for the card, so I included the Crop option to save the image of the desired size.

First of all, I want to thank the following Internet users, articles and libraries who gave me many ideas on how to do this.

To translate code from C# to Visual Basic:

and:

The App used in the phone is:

Using the Code

Image 1

This is the general view of the program, you can use a smartphone as an IP Camera Image 2, use a Webcamera Image 3 or load a picture file Image 4 or Exit Image 5.

In the region "Public Variables", you can configure the behavior of the program.

#Region "Public Variables"
    Public GnGetVarsFromConfig = True
    Public GnPathPhoto As String = "C:\Work\Images\"
    Public GnFilePhoto As String = "TryPhoto"
    Public GnFileExtension As String = "Png"
    Public GnLogRequiresIp As Boolean = True
    Public GnIpCamera As String = "http://192.168.0.4:8080/video"
    Public LogExitSave As Boolean = True
    Public LogSave2File As Boolean = True
    Public LogSaveDefault As Boolean = False
    Public LogRealExit As Boolean = True
    Public GnLogNotExit As Boolean = True
    Public LogJustLoadFile As Boolean = False
    Public LogLoadFileVisible As Boolean = True
    Public LogCropit As Boolean = True
    'Crop dimesions
    Public IntCropWidth As Integer = 340
    Public IntcropHeight As Integer = 454

#End Region

where:

  • GnGetVarsFromConfig: This variable allows the program to read all the configuration variables from the MultipleCameraPhoto.exe.config, to execute it at your convenience.
  • GnPathPhoto: It is the path where the photograph will be saved.
  • GnFilePhoto: It is the default name of the file to save.
  • GnFileExtension: It is the type of file that you want to save (extension).
  • GnLogRequiresIp: If the TextBox containing the IP address is displayed.
  • GnIpCamera: It is IP address of the camera (Important: you must add "/video" at the end of the IP address, to work with the IP WebCam APP).
  • LogExitSave: This variable lets you know if the program ended by the save button or exit button, this is to use it in the program that calls it to refresh the photo.
  • LogSave2File: This variable determines if the photograph is saved using the file dialog to find the path and give it a name.
  • LogSaveDefault: This variable determines if the photograph is saved with the path given in the variable GnPathPhoto, the name given in the variable GnFilePhoto and with the extension given in the variable GnFileExtension.
    Note: The variable LogSave2File has a precedence over this variable, that is, if LogSave2File is set to true, it will display the file save dialog, ignoring the function of this variable.
  • LogRealExit: If this variable is true, the program will close at the end of the execution, otherwise it will only be hidden so that the program that calls it can access the results before closing it.
  • GnLogNotExit: If this variable is true, the program will not come out and will be available to take another picture, this is useful if you want to take several pictures before finishing the program.
  • LogJustLoadFile: This variable determines whether only the file upload button is shown, ignoring the camera buttons.
  • LogLoadFileVisible: This variable determines whether the file upload button is displayed.
  • LogCropit: If the variable is in true, the final photo is cropped to the dimensions specified in the variables IntCropWidth and IntcropHeight, additionally, it will show a yellow box with the dimensions that are specified to cut the image in the center of the picture box.
  • IntCropWidth: Width of the box to be cut.
  • IntcropHeight: Height of the box to be cut.

You can include these variables in your own configuration file:

<!-- Public Variables Definition -->
<add key="GnPathPhoto" value="C:\Work\Images\"/>
<add key="GnFilePhoto" value="TryPhoto"/>
<add key="GnFileExtension" value="Png"/>
<add key="GnLogRequiresIp" value="True"/>
<add key="GnIpCamera" value="http://192.168.0.4:8080/video"/>
<add key="GnLogExitSave" value="True"/>
<add key="GnLogSave2File" value="True"/>
<add key="GnLogSaveDefault" value="False"/>
<add key="GnLogRealExit" value="True"/>
<add key="GnLogNotExit" value="True"/>
<add key="GnLogJustLoadFile" value="False"/>
<add key="GnLogLoadFileVisible" value="True"/>
<add key="GnLogCropit" value="False"/>
<!-- Crop dimesions-->
<add key="GnIntCropWidth" value="340"/>
<add key="GnIntcropHeight" value="454"/>
<!-- End Of Public Variables Definition -->

If you wish, you can use this source code as a Visual Basic class library, for this, you have to make these changes in the project.

Original settings:

Image 6

Change these settings to:

Image 7

Compile, this generates an error:

Image 8

Double click on the error and the editor takes you to the Application Designer file with all these errors:

Image 9

Edit this file and leave it as follows:

'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.42000
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On

Namespace My
    
    'NOTE: This file is auto-generated; do not modify it directly.  To make changes,
    ' or if you encounter build errors in this file, go to the Project Designer
    ' (go to Project Properties or double-click the My Project node in
    ' Solution Explorer), and make changes on the Application tab.
    '
    Partial Friend Class MyApplication
        
        <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
        Public Sub New()

        End Sub

    End Class
End Namespace

Save it and recompile.

Here is an example of how to use this program as a class Library.

Imports MultipleCameraPhoto

Public Class <YourClass>
    .
    .


#Region "Buttons"
    
    Private Sub BtnTakePicture_Click(sender As Object, e As EventArgs) _
                  Handles BtnTakePicture.Click
        Dim StrFileName As String = StrFotoIdOrder
        'WHERE PicBoxPhoto is the picture box of your application
        CapturaFoto(StrPathFotos, StrFileName, "Png", PicBoxPhoto)
    End Sub

    Private Sub BtnLoadPicture_Click(sender As Object, e As EventArgs) _
               Handles BtnLoadPicture.Click
          'WHERE PicBoxPhoto is the picture box of your application
      FindLoadPictures(StrPathFotos, StrFotoIdOrder, ".Png", PicBoxPhoto)
    End Sub

#End Region
    
#Region "Photos"
 
    '***********************************************************
    '*                          Photos                         *
    '***********************************************************
    
    Private Sub CapturePhoto(ByVal StrPathFileName As String, _
    ByVal StrFileName As String, ByVal StrExtension As String, ByRef PictureBox As PictureBox)
        Dim FrnCapturePhoto As New TakeAPicture
        'Dar Parametros
        FrnCapturePhoto.GnGetVarsFromConfig = False
        FrnCapturePhoto.GnLogNotExit = False
        FrnCapturePhoto.GnPathPhoto = StrPathFileName
        FrnCapturePhoto.GnFilePhoto = StrFileName
        FrnCapturePhoto.GnFileExtension = StrExtension
        FrnCapturePhoto.GnLogRequiresIp = False
        FrnCapturePhoto.GnIpCamera = configApp.GetValue("IpCamera", GetType(System.String))
        FrnCapturePhoto.GnLogExitSave = False
        FrnCapturePhoto.GnLogSave2File = False
        FrnCapturePhoto.GnLogSaveDefault = True
        FrnCapturePhoto.GnLogRealExit = False
        FrnCapturePhoto.GnLogJustLoadFile = False
        FrnCapturePhoto.GnLogLoadFileVisible = True
            
        FrnCapturePhoto.ShowDialog()
        If FrnCapturePhoto.GnLogExitSave Then
            LoadPhoto(StrPathFileName, StrFileName + "." + StrExtension, PictureBox)
        End If
    End Sub
    
    Private Sub FindLoadPictures(ByVal StrPathFileName As String, _
    ByVal StrFileName As String, ByVal StrExtension As String, ByVal PictureBox As PictureBox)
        Dim FrnCapturePhoto As New TakeAPicture
        'Dar Parametros
        FrnCapturePhoto.GnGetVarsFromConfig = False
        FrnCapturePhoto.GnPathPhoto = StrPathFileName
        FrnCapturePhoto.GnFilePhoto = StrFileName
        FrnCapturePhoto.GnFileExtension = StrExtension
        FrnCapturePhoto.GnLogRequiresIp = False
        FrnCapturePhoto.GnIpCamera = configApp.GetValue("IpCamera", GetType(System.String))
        FrnCapturePhoto.GnLogExitSave = False
        FrnCapturePhoto.GnLogSave2File = False
        FrnCapturePhoto.GnLogSaveDefault = True
        FrnCapturePhoto.GnLogRealExit = False
        FrnCapturePhoto.GnLogJustLoadFile = True
        FrnCapturePhoto.GnLogLoadFileVisible = True
        FrnCapturePhoto.ShowDialog()
        If FrnCapturePhoto.GnLogExitSave Then
            CargaFoto(StrPathFileName, StrFileName + "." + StrExtension, PictureBox)
        End If
    End Sub
    
    Private Sub LoadPhoto(ByVal StrPathFileName As String, ByVal StrFileName As String, _
                          ByRef PictureBox As PictureBox)
        Dim fs As System.IO.FileStream
        Dim NewPic As New PictureBox
        Try
            If System.IO.File.Exists(StrPathFileName & StrFileName) Then
                ' Specify a valid picture file path on your computer.
                fs = New System.IO.FileStream(StrPathFileName & StrFileName, _
                     IO.FileMode.Open, IO.FileAccess.Read)
                NewPic.Image = System.Drawing.Image.FromStream(fs)
                fs.Close()
            Else
                fs = New System.IO.FileStream(StrPathFileName & "NoPictureFound.png", _
                     IO.FileMode.Open, IO.FileAccess.Read)
                NewPic.Image = System.Drawing.Image.FromStream(fs)
                fs.Close()
            End If
        Catch ex As Exception
            'MessageBox.Show("Error, LoadPhoto: " & ex.Message, _
                "LoadPhoto: " & Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        PictureBox.Image = NewPic.Image
        PictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        NewPic.Dispose()
        NewPic = Nothing
    End Sub

Points Of Interest

I didn't try a real IP Web cam, but I think it may work.

History

  • Version 1.0

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

evillegas1961
Unknown
No Biography provided

Comments and Discussions

 
QuestionMissing Download Pin
Member 1391013912-Jun-19 1:16
memberMember 1391013912-Jun-19 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 7 Jun 2019

Stats

3.3K views
182 downloads
4 bookmarked