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
This is the general view of the program, you can use a smartphone as an IP Camera , use a Webcamera or load a picture file or Exit .
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
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:
<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"/>
<add key="GnIntCropWidth" value="340"/>
<add key="GnIntcropHeight" value="454"/>
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:
Change these settings to:
Compile, this generates an error:
Double click on the error and the editor takes you to the Application Designer file with all these errors:
Edit this file and leave it as follows:
Option Strict On
Option Explicit On
Namespace My
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
CapturaFoto(StrPathFotos, StrFileName, "Png", PicBoxPhoto)
End Sub
Private Sub BtnLoadPicture_Click(sender As Object, e As EventArgs) _
Handles BtnLoadPicture.Click
FindLoadPictures(StrPathFotos, StrFotoIdOrder, ".Png", PicBoxPhoto)
End Sub
#End Region
#Region "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
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
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
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
"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