在其中一个移动网站中,我创建了一个网页,其中我使用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连接和下载图片...
的 的 ** * ** * ** * 的** * * 更新的代码SNIPPET * ** * ** * ** * ** * ** *
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
{
}
答案 0 :(得分:0)
您的移动网站需要很长时间才能回复。您显然正在访问端口80(从您的代码段中可以看到),因此如果您可以使用浏览器访问您的网站,则代码没有任何问题。您遇到某种网络延迟问题。
以防万一,做:
WebClient.DownloadFile("UrlToImage");
应该可以正常工作。
答案 1 :(得分:0)
你能发布一些示例代码吗?
过去我使用过以下代码,但从来没有任何问题。
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之间的int。根据你的需要,50-70是一个很好的起点。