2010-11-07 53 views
0

我有一個監控系統,我想在報警觸發時保存攝像機的快照。我已經嘗試了很多方法來做到這一點......並且它都工作正常。需要幫助。當從IP攝像機流式傳輸圖像時需要幫助。

string ImageName = @"E:\snapshot\pic" + imageid + ".jpg"; 
WebClient webclient = new WebClient(); 
webclient.Credentials = new NetworkCredential("admin", "pass"); 
Uri url = new Uri("http://" + ip + "/cgi-bin/cmd/encoder?SNAPSHOT"); 
webclient.DownloadFileAsync(url, ImageName); 
webclient.Dispose(); 

來自凸輪的圖像是(1280 * 1024)。我想裁剪圖像來獲得(500 * 500)像素

private void button2_Click(object sender, EventArgs e) 
    { 
     string ImageFrom = @"c:\3.jpg"; 
     byte[] imageData = ReadFile(ImageFrom); 
     byte[] data = CropPicture(imageData, 500, 500); 
     SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True"); 
     string qry = "insert into val (id,img) values (@OriginalPath, @ImageData)"; 
     SqlCommand SqlCom = new SqlCommand(qry, cn); 
     SqlCom.Parameters.Add(new SqlParameter("@OriginalPath",(object)"123")); 
     SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)data)); 
     cn.Open(); 
     SqlCom.ExecuteNonQuery(); 
    } 
    byte[] ReadFile(string sPath) 
    { 
     byte[] data = null; 
     FileInfo fInfo = new FileInfo(sPath); 
     long numBytes = fInfo.Length; 
     FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read); 
     BinaryReader br = new BinaryReader(fStream); 
     data = br.ReadBytes((int)numBytes); 
     return data; 
    } 
    public static byte[] CropPicture(byte[] imgFile, int targetW, int targetH) 
    { 
     Image imgPhoto = Image.FromStream(new MemoryStream(imgFile)); 
     int targetX = (imgPhoto.Width - targetW)/2; 
     int targetY = (imgPhoto.Height - targetH)/2; 
     Bitmap bmpPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb); 
     bmpPhoto.SetResolution(80, 60); 
     Graphics gfxPhoto = Graphics.FromImage(bmpPhoto); 
     gfxPhoto.SmoothingMode = SmoothingMode.AntiAlias; 
     gfxPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     gfxPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality; 
     gfxPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel); 
     MemoryStream mm = new MemoryStream(); 
     bmpPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg); 
     // Dispose of all the objects to prevent memory leaks 
     imgPhoto.Dispose(); 
     bmpPhoto.Dispose(); 
     gfxPhoto.Dispose(); 
     return mm.GetBuffer(); 
    } 

然後將其插入到SQL數據庫 我有一個代碼裁剪我知道如何插入圖片到SQL數據庫image.and 但它都需要將圖像作爲電腦中的文件讀取。 我流的圖像,然後將其保存 然後把它和作物,並將其插入到數據庫 請任何一個可以告訴我怎麼去流,而無需將其保存

回答

0

您應該能夠使用類似這...(未測試)

Image img = Image.FromStream((new WebClient()).OpenRead("a")); 
+0

什麼「a」指的是? – Ramah 2010-12-15 12:11:30

+0

要下載的圖像的網址,不知道爲什麼我把「一個」! – 2010-12-15 15:49:03