从URL获取图像但未完全加载

时间:2012-02-27 08:52:22

标签: c# image http httpwebrequest httpwebresponse

我正在尝试从URL获取图像,但是当我将其保存到文件时,它只是实际图像的一半!我搜索了许多网站和解决方案,如HttpWebRequest.BeginGetResponse,因为我认为这是因为我必须缓冲数据,但它不起作用。我不知道我的代码的哪一部分是错的并且出现了这个问题!:(

这是从URL检索图像的代码:

 public static Bitmap GetImageFromUrl(string url)
    {
        string RefererUrl = string.Empty;
        int TimeoutMs = 22 * 1000;
        string requestAccept = "*/*";
        string UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";

        Bitmap img = null;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.UserAgent = UserAgent;
        request.Timeout = TimeoutMs;
        request.ReadWriteTimeout = TimeoutMs * 6;
        request.Accept = requestAccept;

        if (!string.IsNullOrEmpty(RefererUrl))
        {
            request.Referer = RefererUrl;
        }

        try
        {
            WebResponse wResponse = request.GetResponse();
            using (HttpWebResponse response = wResponse as HttpWebResponse)
            {
                Stream responseStream = response.GetResponseStream();
                img = new Bitmap(responseStream);
                response.Close();
            }
        }
        catch (Exception)
        {
        }
        return img;
    }

这是将图像保存在File:

中的代码
        byte[] imgBytes = tile.Image;
        using (MemoryStream ms = new MemoryStream(imgBytes))
        {
            using (Image img = Image.FromStream(ms))
            {
                ms.Dispose();
                Bitmap tempBmp = new Bitmap(img);
                img.Dispose();

                string activeDir = Environment.CurrentDirectory;
                string newPath = System.IO.Path.Combine(activeDir, "Images");
                System.IO.Directory.CreateDirectory(newPath);

                newPath = System.IO.Path.Combine(newPath, tile.TileType.ToString());
                System.IO.Directory.CreateDirectory(newPath);

                newPath = System.IO.Path.Combine(newPath, tile.Zoom.ToString());
                System.IO.Directory.CreateDirectory(newPath);

                newPath = System.IO.Path.Combine(newPath, tile.X.ToString());
                System.IO.Directory.CreateDirectory(newPath);

                newPath = System.IO.Path.Combine(newPath, tile.Y.ToString());
                System.IO.Directory.CreateDirectory(newPath);

                string newFileName = "tile.png";
                newPath = System.IO.Path.Combine(newPath, newFileName);

                tempBmp.Save(newPath, ImageFormat.Png);
                tempBmp.Dispose();

2 个答案:

答案 0 :(得分:1)

您的代码看起来过于复杂,您真的需要读取图像然后重新保存吗? 为什么你不能直接保存下载的图像,如下所示:

        public static void GetImageFromUrl(string url)
    {
        string RefererUrl = string.Empty;
        int TimeoutMs = 22 * 1000;
        string requestAccept = "*/*";
        string UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";

      //  Bitmap img = null;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.UserAgent = UserAgent;
        request.Timeout = TimeoutMs;
        request.ReadWriteTimeout = TimeoutMs * 6;
        request.Accept = requestAccept;

        if (!string.IsNullOrEmpty(RefererUrl))
        {
            request.Referer = RefererUrl;
        }

        try
        {
            WebResponse wResponse = request.GetResponse();
            using (HttpWebResponse response = wResponse as HttpWebResponse)
            {
                Stream responseStream = response.GetResponseStream();
                BinaryReader br = new BinaryReader(responseStream);

                FileStream fs = new FileStream(@"c:\pst\1.jpg", FileMode.Create, FileAccess.Write);

                const int buffsize = 1024;
                byte[] bytes = new byte[buffsize];
                int totalread = 0;

                int numread = buffsize;
                while (numread != 0)
                {
                    // read from source
                    numread = br.Read(bytes, 0, buffsize);
                    totalread += numread;

                    // write to disk
                    fs.Write(bytes, 0, numread);
                }

                br.Close();
                fs.Close();


                response.Close();
            }
        }
        catch (Exception)
        {
        }
    } 

您当然应该将其拆分为方法并设置正确的返回值

答案 1 :(得分:1)

您不应在Dispose()ms个对象上调用img,直到最后将其保存到文件中。

它们甚至在自己的using部分中声明,因此您根本不需要在它们上面调用Dispose(),因为它会自动放置在每个使用过的块的出口处(这是真的是首先使用using的主要原因!)。