c# - Slow performance in reading from stream .NET -
i have monitoring system , want save snapshot camera when alarm trigger. have tried many methods that…and it’s working fine , stream snapshot camera save jpg in pc…. picture (jpg format,1280*1024,140kb)..that’s fine problem in application performance... app need 20 ~30 seconds read steam, that’s not acceptable coz method called every 2 second .i need know wrong code , how can faster that. ? many 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);
i doubt code cause of problem, @ least first method call (but read further below).
technically, produce bitmap without saving memory buffer first, or if don't need display image well, can save raw data without ever constructing bitmap, that's not going in terms of multiple seconds improved performance. have checked how long takes download image url using browser, wget, curl or whatever tool, because suspect going on encoding source.
something should clean resources; close stream properly. can potentially cause problem if call method regularly, because .net open few connections same host @ 1 point.
// make sure stream gets closed once we're done using (stream stream = resp.getresponsestream()) { // larger buffer size benefitial, it's not going // make significant difference. while ((read = stream.read(buffer, total, 1000)) != 0) { total += read; } }
Comments
Post a Comment