Click here to Skip to main content
15,898,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi sir

I am working in Student management system. It works fine. But I need to created the Student ID using their Image which is captured by Web Cam

I got sample code from Mr.Mudhasar (http://www.aspsnippets.com/), I have changed as per my needs. Every thing is fine. But I could not name the image by studnetID and I could not capture the path in my text box in web page.

Currently image saved as datetime name.

Below is my code

Sorry for poor english.

Pls Help me

Maideen



Vb.Code

VB
Imports dbConnection
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Imports System.Configuration
Imports System.Web.UI.Control
Imports System.Web.UI.ScriptManager
Imports System.Data.OleDb
Imports System.IO
Imports System.Web.UI.HtmlControls.HtmlGenericControl
Imports Microsoft.Reporting.WebForms
Imports System.Globalization
Imports System.Web.Services

Partial Class mod_Student_ID_ID_Card
    Inherits System.Web.UI.Page
    Dim cmd As New SqlCommand
    Dim data As SqlDataReader
    Dim cls As New cls_Student_Details

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.txtID.Text = Session("stud_id")
            loaddata()
            captureLive()
        End If

    End Sub

    Private Sub loaddata()
        Dim errmsg As String = String.Empty
        Dim ds As DataSet = cls.loaddata(Me.txtID.Text)
        If ds.Tables.Count > 0 Then
            If ds.Tables(0).Rows.Count > 0 Then
                Me.txtSID.Text = ds.Tables(0).Rows(0).Item("sid").ToString
                Me.txtName.Text = ds.Tables(0).Rows(0).Item("name").ToString
                Me.txtCourseCode.Text = ds.Tables(0).Rows(0).Item("coursecode").ToString
                Me.txtIntake.Text = ds.Tables(0).Rows(0).Item("intakem").ToString & " - " & ds.Tables(0).Rows(0).Item("intakey").ToString
                Me.txtNRIC.Text = (ds.Tables(0).Rows(0).Item("nric").ToString)
                Me.txtAddress.Text = ds.Tables(0).Rows(0).Item("caddress").ToString

            Else
                errmsg = "Error Loading Data"

            End If
        Else
            errmsg = "Error Loading Data"
        End If

    End Sub
    Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Response.Redirect("default.aspx")
    End Sub

    Sub captureLive()
        If Request.InputStream.Length > 0 Then
            Using reader As New StreamReader(Request.InputStream)
                Dim hexString As String = Server.UrlEncode(reader.ReadToEnd())

                Dim imageName As String = DateTime.Now.ToString("dd-MM-yy hh-mm-ss")  '' ( I need image name by Student ID)

                Dim imagePath As String = String.Format("../Captures/{0}.png", imageName)
                File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString))
                Session("CapturedImage") = ResolveUrl(imagePath)
            End Using
        End If
    End Sub

    <WebMethod(EnableSession:=True)> _
    Public Shared Function GetCapturedImage() As String
        Dim url As String = HttpContext.Current.Session("CapturedImage").ToString()
        HttpContext.Current.Session("CapturedImage") = Nothing
        Return url
    End Function
    Private Shared Function ConvertHexToBytes(hex As String) As Byte()
        Dim bytes As Byte() = New Byte(hex.Length / 2 - 1) {}
        For i As Integer = 0 To hex.Length - 1 Step 2
            bytes(i / 2) = Convert.ToByte(hex.Substring(i, 2), 16)
        Next
        Return bytes
    End Function

    Private Sub SaveMode()
        ' Read the file and convert it to Byte Array 

        Dim filePath As String = Me.txtImageName.Text '(Here I need Path and file name in order to save in Database)

        Dim filename As String = Path.GetFileName(filePath)
        Dim ext As String = Path.GetExtension(filename)
        Dim contenttype As String = String.Empty

        'Set the contenttype based on File Extension
        Select Case ext
            Case ".doc"
                contenttype = "application/vnd.ms-word"
                Exit Select
            Case ".docx"
                contenttype = "application/vnd.ms-word"
                Exit Select
            Case ".xls"
                contenttype = "application/vnd.ms-excel"
                Exit Select
            Case ".xlsx"
                contenttype = "application/vnd.ms-excel"
                Exit Select
            Case ".jpg"
                contenttype = "image/jpg"
                Exit Select
            Case ".png"
                contenttype = "image/png"
                Exit Select
            Case ".gif"
                contenttype = "image/gif"
                Exit Select
            Case ".pdf"
                contenttype = "application/pdf"
                Exit Select
            Case ".ico"
                contenttype = "image/ico"
                Exit Select
            Case ".bmp"
                contenttype = "image/bmp"
                Exit Select
        End Select
        If contenttype <> String.Empty Then
            Dim fs As Stream = FileUpload1.PostedFile.InputStream
            Dim br As New BinaryReader(fs)
            Dim bytes As Byte() = br.ReadBytes(fs.Length)

            'insert the file into database 

            Dim strQuery As String = "insert into tbl_student_image (imagename, imagetype, imagedata) values (@imagename, @imagetype, @imagedata)"
            Dim cmd As New SqlCommand(strQuery)
            cmd.Parameters.Add("@imagename", SqlDbType.VarChar).Value = filename
            cmd.Parameters.Add("@imagetype", SqlDbType.VarChar).Value = contenttype
            cmd.Parameters.Add("@imagedata", SqlDbType.Binary).Value = bytes

            InsertUpdateData(cmd)


            lblMessage.ForeColor = System.Drawing.Color.Green
            lblMessage.Text = "File Uploaded Successfully"
        Else
            lblMessage.ForeColor = System.Drawing.Color.Red
            lblMessage.Text = "File format not recognised. Upload Image/Word/PDF/Excel formats"
        End If
    End Sub
    Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean


        Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ControlDataConnection").ConnectionString
        Dim con As New SqlConnection(strConnString)
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            Return True
        Catch ex As Exception
            'Response.Write(ex.Message)
            Return False
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Function

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        SaveMode()
    End Sub
End Class
Posted
Updated 4-Jan-16 2:35am
v2

1 solution

Just change the code to save the image with name same as Student ID.
Something like-
VB
Dim imageName As String = Session("stud_id").ToString 

Note: You need to make sure that ``Session("stud_id")`` is available prior to this statement.

To find the path of the file saved in your application folder, you can use
HttpServerUtility.MapPath Method[^]
Something like following may help-
VB
HttpContext.Current.Server.MapPath("../Captures/"+Session["stud_id"].ToString+".png")


Hope, it helps :)
 
Share this answer
 
v2
Comments
ridoy 4-Jan-16 11:59am    
take my 5!
Suvendu Shekhar Giri 4-Jan-16 13:02pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900