【问题标题】:How to save the image to the database after streaming it from an IP camera?从 IP 摄像机流式传输图像后如何将图像保存到数据库?
【发布时间】:2011-07-24 10:34:46
【问题描述】:

我有一个代码可以从 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;
     }
   }

如何结合这两个代码sn-ps来流式传输图像,然后立即将其插入数据库?

【问题讨论】:

    标签: c# sql image streaming


    【解决方案1】:

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

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

    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();
    }
    

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

    【讨论】:

    • 没用。我可以将数据保存在数据库中。但是当我检查图像以确保它以正确的顺序保存时,我无法使用水晶报告打开任何图像。请提供更多建议
    • @Ramah 你是如何从数据库中加载图像的?您可能必须先将字节转换为位图。
    猜你喜欢
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多