How to get image from web cam and store in a SQL Server varbinary(max) column

216 Views Asked by At

I have a project that has a requirement for storing images captured from a webcam into a SQL Server column of type varbinary(max). I can load the webcam image on my page, but I have no idea how to pass it to buffer.

Here is code:

if (btn_submit.Text == "Save")
{
    DAL_Student objsu = new DAL_Student();
    byte[] buffer = null;

    if (rbtfileupload.Checked)
    {
        if (fileupload.HasFile && fileupload.PostedFile.ContentLength > 0)
        {
            buffer = new byte[fileupload.FileContent.Length];
            Stream s = fileupload.FileContent;
            s.Read(buffer, 0, buffer.Length);
        }
        else
        {
            buffer = null;
        }
    }
    else if(rbtlivecamera.Checked)   // I just want image from camera
    {
        if (Request.InputStream.Length > 0)
        {
            using (StreamReader reader = new StreamReader(Request.InputStream))
            {
                string hexString = Server.UrlEncode(reader.ReadToEnd());
                buffer = new byte[hexString.Length];
                File.WriteAllBytes(Server.MapPath(imagePath), ConvertHexToBytes(hexString));

                Session["CapturedImage"] = ResolveUrl(imagePath);
            }
        }
    }

    int returnValue = objsu.Save_Student(string.Empty,
                                         txtname.Text.Trim(),
                                         ddl_gender.SelectedItem.Text.Trim(),
                                         txtfname.Text.Trim() ?? string.Empty,
                                         txtmname.Text.Trim() ?? string.Empty,
                                         txtdob.Text.Trim(),
                                         ddl_religion.SelectedItem.Text.ToString(),
                                         ddl_bloodgrp.SelectedItem.Text.ToString(),
                                         txtfoccu.Text.Trim() ?? string.Empty,
                                         txtemail.Text.Trim() ?? string.Empty,
                                         txtaddate.Text.Trim(),
                                         ddl_class.SelectedItem.Value.ToString(),
                                         ddl_section.SelectedItem.Value.ToString(),
                                         txtrollno.Text.Trim(),
                                         txtaddress.Text.Trim() ?? string.Empty,
                                         txtshortbio.Text.Trim() ?? string.Empty,
                                         txtmono.Text.Trim(),
                                         txtphono.Text.Trim() ?? string.Empty,
                                         "1", buffer, "1", "Add");
    if (returnValue > 0)
    {
        ltr_message.Text = "<div class='alert alert-success alert-dismissible fade show' role='alert'>Student created successfully!<button type = 'button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button></div>";
        ClearControl(this);
    }
}

How to store the webcam image to buffer and pass the buffer into the SQL Server varbinary(max) column?

Please help me out guys..

0

There are 0 best solutions below