C# 如何将图像从IP摄像头流式传输到数据库?

C# 如何将图像从IP摄像头流式传输到数据库?,c#,sql,image,streaming,C#,Sql,Image,Streaming,我有一个代码来流式传输IP摄像头的快照并将其保存在硬盘上。 然后我有另一个代码来读取它并将其存储在ms sql数据库中。 我知道,如果我只是流式传输图像并将其保存在数据库中,而不将其保存在硬盘上并再次读取,速度会更快。但我不知道如何合并这个 获取图像的代码 string sourceURL = "http://" + ip + "/cgi-bin/cmd/encoder?SNAPSHOT"; WebRequest req = (WebRequest)WebRequ

我有一个代码来流式传输IP摄像头的快照并将其保存在硬盘上。 然后我有另一个代码来读取它并将其存储在ms sql数据库中。 我知道,如果我只是流式传输图像并将其保存在数据库中,而不将其保存在硬盘上并再次读取,速度会更快。但我不知道如何合并这个

获取图像的代码

        string sourceURL = "http://" + ip + "/cgi-bin/cmd/encoder?SNAPSHOT";
        WebRequest req = (WebRequest)WebRequest.Create(sourceURL);
        req.Credentials = new NetworkCredential("admin", "123456");
        WebResponse resp = req.GetResponse();
        Stream stream = resp.GetResponseStream();
        Bitmap bmp = (Bitmap)Bitmap.FromStream(stream);
        bmp.Save(ImagePath);
然后,要将图像插入数据库,请执行以下操作:

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("@OriginalPath", (object)id));
  SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData));
  cn.Open();
  SqlCom.ExecuteNonQuery();
  cn.Close();
  cn.Dispose();
  }
  byte[] ReadFile(string sPath)
   {
     using (FileStream fStream = new FileStream(sPath, FileMode.Open,      FileAccess.Read))
     {
      byte[] data = null;
      FileInfo fInfo = new FileInfo(sPath);
      long numBytes = fInfo.Length;
      BinaryReader br = new BinaryReader(fStream);
      data = br.ReadBytes((int)numBytes);
      return data;
     }
   }
Stream stream = resp.GetResponseStream();
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
  int num_bytes = 0;
  byte[] temp = new byte[4096];
  while ((num_bytes = stream.Read(temp, 0, temp.Length)) > 0)
    ms.Write(temp, 0, bytes);
  data = ms.ToArray();
}

如何将这两个代码片段组合起来以流式传输图像,然后立即将其插入数据库?

为什么要将传入的字节转换为位图

您可能只需将该流读取到内存流,然后将其传递到数据库:

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("@OriginalPath", (object)id));
  SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)imageData));
  cn.Open();
  SqlCom.ExecuteNonQuery();
  cn.Close();
  cn.Dispose();
  }
  byte[] ReadFile(string sPath)
   {
     using (FileStream fStream = new FileStream(sPath, FileMode.Open,      FileAccess.Read))
     {
      byte[] data = null;
      FileInfo fInfo = new FileInfo(sPath);
      long numBytes = fInfo.Length;
      BinaryReader br = new BinaryReader(fStream);
      data = br.ReadBytes((int)numBytes);
      return data;
     }
   }
Stream stream = resp.GetResponseStream();
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
  int num_bytes = 0;
  byte[] temp = new byte[4096];
  while ((num_bytes = stream.Read(temp, 0, temp.Length)) > 0)
    ms.Write(temp, 0, bytes);
  data = ms.ToArray();
}

然后将数据传递到数据库。

无效。我可以将数据保存在数据库中。但是,当我检查图像以确保其以正确的顺序保存时,我无法使用crystal report打开任何图像。还有其他建议吗please@Ramah如何从数据库加载图像?您可能必须首先将字节转换为位图。