【问题标题】:Cannot download image using WebClient in ASP.NET无法在 ASP.NET 中使用 WebClient 下载图像
【发布时间】:2011-12-07 12:08:18
【问题描述】:

在其中一个移动站点中,我创建了一个网页,其中我使用 webclient 从主站点(移动主站点)下载图像并使用位图调整大小,并将图像获取到移动站点,主站点的图像路径工作正常,但是当我使用 WebClient 下载图像以调整大小时,我收到以下错误:

CreateThumbnail :System.Net.WebException: Unable to connect to the
   remote server ---> System.Net.Sockets.SocketException: A connection
   attempt failed because the connected party did not properly respond
   after a period of time, or established connection failed because
   connected host has failed to respond 209.59.186.108:80 at
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
   SocketAddress socketAddress) at
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
   Socket s4, Socket s6, Socket& socket, IPAddress& address,
   ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
   Exception& exception)

任何人都可以建议任何解决此问题的方法,我尝试使用以下命令 ping 上述 IP (209.59.186.108):

ping m.keyboardmag.com

它返回以下结果:

Pinging m.keyboardmag.com [209.59.186.108] with 32 byte
Reply from 209.59.186.108: bytes=32 time=233ms TTL=112
Reply from 209.59.186.108: bytes=32 time=237ms TTL=112
Reply from 209.59.186.108: bytes=32 time=230ms TTL=112
Reply from 209.59.186.108: bytes=32 time=231ms TTL=112

仍然无法使用 WebClient 连接和下载图像...

*************更新的代码片段******** ********

if (Request.QueryString["file"] != null)
        {
            string file = Request.QueryString["file"].ToString();
            int lnHeight = Convert.ToInt32(Request.QueryString["height"]);
            int lnWidth = Convert.ToInt32(Request.QueryString["width"]);
            string imgUrl = Request.QueryString["file"].ToString();
            Bitmap bmpOut = null;
            try
            {
                Bitmap loBMP;
                WebClient wb = new WebClient();

                byte[] ret = wb.DownloadData(imgUrl);

                MemoryStream ms = new MemoryStream(ret);
                loBMP = new Bitmap((Stream)ms);
                System.Drawing.Imaging.ImageFormat loFormat = loBMP.RawFormat;
                decimal lnRatio;
                int lnNewWidth = 0;
                int lnNewHeight = 0;
                //-----If the image is smaller than a thumbnail just return it As it is----- 
                if ((loBMP.Width < lnWidth && loBMP.Height < lnHeight))
                {
                    lnNewWidth = loBMP.Width;
                    lnNewHeight = loBMP.Height;
                }
                if ((loBMP.Width > loBMP.Height))
                {
                    lnRatio = (decimal)lnHeight / loBMP.Height;
                    lnNewHeight = lnHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                    if (lnNewWidth > 128)
                    {
                        lnNewWidth = 128;
                    }
                    /*
                    lnRatio = (decimal)lnWidth / loBMP.Width;
                    lnNewWidth = lnWidth;
                    decimal lnTemp = loBMP.Height * lnRatio;
                    lnNewHeight = (int)lnTemp;*/
                }
                else
                {
                    lnRatio = (decimal)lnHeight / loBMP.Height;
                    lnNewHeight = lnHeight;
                    decimal lnTemp = loBMP.Width * lnRatio;
                    lnNewWidth = (int)lnTemp;
                    if (lnNewWidth < 75)
                    {
                        lnNewWidth = 75;
                    }
                }
                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
                if (Path.GetExtension(imgUrl) == "jpg")
                    Response.ContentType = "image/jpeg";
                else if (Path.GetExtension(imgUrl) == "bmp")
                    Response.ContentType = "image/bmp";
                else if (Path.GetExtension(imgUrl) == "png")
                    Response.ContentType = "image/png";
                else if (Path.GetExtension(imgUrl) == "gif")
                    Response.ContentType = "image/gif";

                bmpOut.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("CreateThumbnail :" + ex.ToString());
            }
            finally
            {
            }

【问题讨论】:

  • 请贴出使用WebClient的代码

标签: asp.net sockets webclient webexception


【解决方案1】:

你能发布一些示例代码吗?

过去我使用过以下代码,但从未遇到过任何问题。

WebClient wb = new WebClient();
Image originalImage = Image.FromStream(wb.OpenRead(Url));
Image thumbNail = ImageResize.Crop(originalImage, Width, Height, ImageResize.AnchorPosition.Top);

图片保存代码:

EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, MyQuality);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
originalImage.Save(FilePath, jpegCodec, encoderParams);

MyQuality 是一个介于 0-100 之间的整数。 50-70 是一个很好的起点,具体取决于您的需要。

【讨论】:

  • 您好,我已经上传了我正在使用的 webclient 代码 sn-p,请您检查问题以进行编辑。
  • 嗨@k_man,你能告诉我ImageResize类的命名空间是什么,或者它的自定义类
  • @Abbas,对此感到抱歉,忘记了我在那里。这是我在codeproject.com/KB/GDI-plus/imageresize.aspx 找到的一个很好的帮助类
  • @Abbas,在查看示例代码时,它看起来应该可以工作。我会尝试上面的代码。当我在测试时,我会将 originalImage 保存到硬盘驱动器以验证我下载的内容是否正确(请参阅上面的保存代码)。另外,我认为 url 应该完全符合 url 中的 http://。
【解决方案2】:

您的移动网站响应时间过长。您显然正在访问端口 80(从您的 sn-p 可以看出),因此如果您可以使用浏览器访问您的站点,那么您的代码没有任何问题。您有某种网络延迟问题。

以防万一,这样做:

WebClient.DownloadFile("UrlToImage");

应该可以正常工作。

【讨论】:

  • 如何查看网站是否访问了80端口
  • 你在访问 80 端口,查看 ip 地址后面的 ':80'。那是端口号。同样,如果您可以使用浏览器访问该网站,您应该能够使用您的代码访问它。我觉得很好。你在代理后面吗?也许您需要先通过代理进行身份验证才能连接? WebClient 有一个属性,使用它就是你的情况。
  • @lcarus 我也使用了身份验证,但仍然无法正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-12
  • 2012-10-27
相关资源
最近更新 更多