/// <summary> /// Dowload file form CFServer /// </summary> /// <param name="strURL">ex: http://192.168.2.221/cfserver.fcgi?cmd=getimage&id=1344311451138&type=normal </param> /// <returns></returns> public Image DownloadImageFromUrl(string strURL, ref string exMess) { Image imagesDowload = null; HttpWebRequest ImageWebRequest; HttpWebResponse ImageWebResponse = null; Stream responseStream = null; try { ImageWebRequest = (HttpWebRequest)WebRequest.Create(strURL); ImageWebRequest.AllowWriteStreamBuffering = true; ImageWebRequest.Timeout = 3000; ImageWebResponse = (HttpWebResponse)ImageWebRequest.GetResponse(); responseStream = ImageWebResponse.GetResponseStream(); imagesDowload = Image.FromStream(responseStream); ImageWebResponse.Close(); } catch (Exception ex) { exMess += ex.Message; exMess += "\n"; return(null); } finally { //Close connections //Release objects ImageWebRequest = null; ImageWebResponse = null; responseStream = null; } return(imagesDowload); }
private void DownloadImageFromUrl() { if (!this.IsManager) { using (var globalMutex = new Mutex(false, "Global\\{{2671848c-26d4-4ab1-942f-103f34a3fbbf}}")) { try { if (Monitor.TryEnter(globalMutex)) { Monitor.Exit(lockMute); return; } } catch (Exception ex) { //todo log } } } if (!Monitor.TryEnter(lockMute)) { return; } Image imagesDowload = null; HttpWebRequest ImageWebRequest; HttpWebResponse ImageWebResponse = null; Stream responseStream = null; try { //ImageWebRequest = (HttpWebRequest)WebRequest.Create("http://192.0.2.3/scapture"); string strURLCaptureImage = "http://" + cameraIp + "/scapture"; ImageWebRequest = (HttpWebRequest)WebRequest.Create(strURLCaptureImage); ImageWebRequest.AllowWriteStreamBuffering = true; ImageWebRequest.Timeout = 2000; ImageWebResponse = (HttpWebResponse)ImageWebRequest.GetResponse(); responseStream = ImageWebResponse.GetResponseStream(); imagesDowload = Image.FromStream(responseStream); ImageWebResponse.Close(); CaptureDone((Bitmap)imagesDowload); } catch (Exception ex) { } finally { //Close connections //Release objects ImageWebRequest = null; ImageWebResponse = null; responseStream = null; Monitor.Exit(lockMute); } }