提高下载速度?

时间:2012-12-13 09:40:32

标签: c#

我想使用此代码从相机下载一系列图像。 但是下载很慢,如果我在幻灯片中查看,我可以从图像序列中看到不顺畅。 有什么方法可以提高下载过程的速度吗?

for (int i = 0; i < 100; i++)
{
    string ImagePath = Server.MapPath("\\CCTV_Files\\LogFile\\" + String.Format("{0:yyyyMMdd_hhmmss}", DateTime.Now) + ".jpg");
    string ip = "http://IP.address.com:80/snapshot.cgi";

    string sourceURL = ip;
    WebRequest req = (WebRequest)WebRequest.Create(sourceURL);
    req.Credentials = new NetworkCredential("usr", "pwd");
    WebResponse resp = req.GetResponse();
    Stream stream = resp.GetResponseStream();

    Bitmap bmp = (Bitmap)Bitmap.FromStream(stream);
    bmp.Save(ImagePath);
}

1 个答案:

答案 0 :(得分:2)

  

有什么方法可以提高下载过程的速度吗?

首先,您可以在下载时停止将数据加载为Bitmap。只需将原始字节从正文保存到磁盘即可。

请注意,您还应该使用using语句作为回复,以便正确关闭。

另请注意,如果您使用WebClient,那么很可能会使代码更简单 - 只需使用WebClient.DownloadFile并让框架完成工作。

相关问题