c# - How to save the image to the database after streaming it from an IP camera? -


I have a code to stream snapshots from an IP camera and save it to hard disk. Then I have another code to read it and store it in the MS-SQL database. I know that it will be faster if I stream the image and save it to the database and save it to the hard disk and again it Read But I do not know how to merge it.

Code to get the image

  string sourceURL = "http: //" + ip + "/ cgi-bin? / Cmd / encoder snapshot"; WebRequest req = (WebRequest) WebRequest.Create (sourceURL); Req.Credentials = New network credentials ("Admin", "123456"); WebResponse resp = req.GetResponse (); Stream stream = resp.GetResponseStream (); Bitmap BMP = (bitmap) bitmap Frostream (stream); Bmp.Save (ImagePath);   

Then to insert an image in the database:

  byte [] imageData = ReadFile (ImageName); (Using SqlConnection cn = new SqlConnection (constr)) {string qry = "Update vaiolations set val_image = @ ImageData, valid = 1 where id = @ OriginalPath"; SqlCommand SqlCom = New SqlCommand (qry, cn); SqlCom.Parameters.Add (New SqlParameter ("@Perfect", (Object ID)); SqlCom.Parameters.Add (new SqlParameter ("@ ImageData", (object) imageData)); Cn.Open (); SqlCom.ExecuteNonQuery (); Cn.Close (); Cn.Dispose (); } Byte [] using the Readfile (String Aspath) {filestream fstream = new flamestream (aspath, flammod.open, file entry reads)) {byte [] data = null; FileinfoFINO = new fileinfo (aspath); Long numbytes = fInfo.Length; Binaryreader BR = New Binary Reader (Fstream); Data = br.ReadBytes ((int) numBytes); Return data; }}   

How can I add these two code snippets to stream the image and then insert it into the database immediately?

Why are you converting incoming bytes to bitmap?

You can probably read the stream in a memory stream, and pass it on to the database:

  stream stream = resp.GetResponseStream (); Byte [] data; (Using MemoryStream ms = new MemoryStream ()) {int num_bytes = 0; Byte [] Temp = new byte [40 9]; While ((num_bytes = stream.Read (temp, 0, temp.Length)) gt; ms.Write (temp, 0, bytes); data = MS ToArray ();}   

Then pass data to your database.

Comments