0

I am trying to capture a photo with a webcam and then save it on the localdb. After that i want to asign it to a user and each time a new photo is taken to rewrite the original photo on the server and all the references.

Capture photo code:

public ActionResult Capture()
        {
            var stream = Request.InputStream;
            string dump;

            using (var reader = new StreamReader(stream))
            {
                dump = reader.ReadToEnd();

                DateTime nm = DateTime.Now;

                string date = nm.ToString("yyyymmddMMss");

                var path = Server.MapPath("~/WebImages/" + date + "test.jpg");

                System.IO.File.WriteAllBytes(path, String_To_Bytes2(dump));

                ViewData["path"] = date + "test.jpg";

                Session["val"] = date + "test.jpg";
            }

            return View("Index"); 
        }

The problem I am facing is that i am not sure how i can rewrite it each time and change all references in the db to it. Any help is welcomed! Thank you!

xDevil
  • 147
  • 1
  • 2
  • 12
  • There are two things, first save the images in folder like you are just doing it and second save images in binary in database. Which one you looking among these two? – Anil Panwar Dec 31 '15 at 06:38
  • i would like to save the image in the database and rewrite it and hopefully all references will change if it is the same name! – xDevil Dec 31 '15 at 06:39
  • my code saves the image in the folder so i only need to save it in the database – xDevil Dec 31 '15 at 06:42
  • While you are saving images to the folder you should rename (unique)that image name and at the same time save image name to database for that particular user and when you would like to change the image for the user then get the saved image name for that user from database and delete that from saved folder using name of the image and upload new image to folder and save its name to database against the user – Anil Panwar Dec 31 '15 at 06:47

1 Answers1

0

You can also add column Image in user table of binary type and save the binary format image to that column.

Use below code to convert image to binary format and the converted bytes that you have to save in the column.

   public byte[] ConvertToBytes(HttpPostedFileBase image)
 {
    byte [] imageBytes = null;
    BinaryReader reader = new        BinaryReader
   (image.InputStream);
    imageBytes = reader.ReadBytes
    ((int )image.ContentLength);
     return imageBytes;
  }

For complete tutorial please refer link http://www.c-sharpcorner.com/UploadFile/b696c4/how-to-upload-and-display-image-in-mvc/

Anil Panwar
  • 2,592
  • 1
  • 11
  • 24