2
我有一個代碼來流從IP攝像機的快照,並將其保存在硬盤上。 然後我有另一個代碼來讀取它並將其存儲在ms-sql數據庫中。 我知道如果我只是流式傳輸圖像並將其保存在數據庫中而不保存在硬盤上並再次讀取,速度會更快。但我不知道如何合併這個。如何從IP攝像機傳輸圖像後將圖像保存到數據庫?
代碼來獲取圖像
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 [email protected] ,valid=1 where [email protected] ";
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;
}
}
我如何結合這兩種代碼片段流中的圖像,然後立即將其插入數據庫?
沒有工作。我可以將數據保存在數據庫中。但是當我檢查圖像以確保它以正確的順序保存時,我無法使用水晶報告打開任何圖像。更多建議請 – Ramah 2011-04-19 13:32:53
@Ramah你好嗎從數據庫加載圖像?您可能必須首先將字節轉換爲位圖。 – 2011-04-20 08:12:34