views:

164

answers:

5

I have a monitoring system and I want to save a snapshot from a camera when alarm trigger. I have tried many methods to do that…and it’s all working fine , stream snapshot from the camera then save it as a jpg in the pc…. picture (jpg format,1280*1024,140KB)..That’s fine But my problem is in the application performance... The app need about 20 ~30 seconds to read the steam, that’s not acceptable coz that method will be called every 2 second .I need to know what wrong with that code and how I can get it much faster than that. ? Many thanks in advance Code:

string sourceURL = "http://192.168.0.211/cgi-bin/cmd/encoder?SNAPSHOT";
byte[] buffer = new byte[200000];
int read, total = 0;
WebRequest req = (WebRequest)WebRequest.Create(sourceURL);
req.Credentials = new NetworkCredential("admin", "123456");
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
while ((read = stream.Read(buffer, total, 1000)) != 0)
  {
      total += read;
  }
Bitmap bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0,total));
string path = JPGName.Text+".jpg";
bmp.Save(path);
+1  A: 

I cannot try the network behavior of the WebResponse stream, but you handle the stream twice (once in your loop and once with your memory stream).

I don't thing that's the whole problem but I'd give it a try:

     string sourceURL = "http://192.168.0.211/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);
     string path = JPGName.Text + ".jpg";
     bmp.Save(path);
Florian Reischl
+3  A: 

I very much doubt that this code is the cause of the problem, at least for the first method call (but read further below).

Technically, you could produce the Bitmap without saving to a memory buffer first, or if you don't need to display the image as well, you can save the raw data without ever constructing a Bitmap, but that's not going to help in terms of multiple seconds improved performance. Have you checked how long it takes to download the image from that URL using a browser, wget, curl or whatever tool, because I suspect something is going on with the encoding source.

Something you should do is clean up your resources; close the stream properly. This can potentially cause the problem if you call this method regularly, because .NET will only open a few connections to the same host at any one point.

// Make sure the stream gets closed once we're done with it
using (Stream stream = resp.GetResponseStream())
{
    // A larger buffer size would be benefitial, but it's not going
    // to make a significant difference.
    while ((read = stream.Read(buffer, total, 1000)) != 0)
    {
        total += read;
    }
}
Thorarin
+1  A: 

Try to read bigger pieces of data, than 1000 bytes per time. I can see no problem with, for example,

read = stream.Read(buffer, 0, buffer.Length);
Dimps
except that this could read faster than data is written into stream, which will cause data loses. But the main idea, anyway, it to read bigger chunks of bytes at once.
Dimps
A: 

Try this to download the file.

using(WebClient webClient = new WebClient())
{
    webClient.DownloadFile("http://192.168.0.211/cgi-bin/cmd/encoder?SNAPSHOT", "c:\\Temp\myPic.jpg");
}

You can use a DateTime to put a unique stamp on the shot.

Dan
A: 

many thanks for all i tried all this codes all of it is working fine but taking the same time


Try to read bigger pieces of data, than 1000 bytes per time. I can see no problem with, for example,

read = stream.Read(buffer, 0, buffer.Length);

i can't make it "buffer.Length" but i tried bigger pieces of data and that's didn't save much time.it's saving about 0~3 second ..many thanks Dimps


// Make sure the stream gets closed once we're done with it using (Stream stream = resp.GetResponseStream()) { // A larger buffer size would be benefitial, but it's not going // to make a significant difference. while ((read = stream.Read(buffer, total, 1000)) != 0) { total += read; }

}

i added stream.Flush(); stream.Close(); at the end of my code and that's didn't change any thing .many thanks Thorarin


string sourceURL = "http://192.168.0.211/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); string path = JPGName.Text + ".jpg";

bmp.Save(path);

that code is working fine but it take the same time as my code...many thanks Florian Reischl


Try this to download the file.

using(WebClient webClient = new WebClient()) { webClient.DownloadFile("http://192.168.0.211/cgi-bin/cmd/encoder?SNAPSHOT", "c:\Temp\myPic.jpg"); }

You can use a Date-time to put a unique stamp on the shot.

I'm using a DateTime to get the excat time ..and i tried this code but after editing the Credentials to get it work and it's taking the same time.


many thanks to all friends and hope if there is any more idea's

Ramah