0

In one of the mobile site, i created a webpage, wherein i am using webclient to the download the image from the main site(main site of mobile) and resize using bitmap, and get the image to the mobile site, the image path for the main site works fine, but when i use the WebClient to download the image to resize, i gets the following error:

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)

can anyone please suggest any solution to this problem i tried to ping the above IP (209.59.186.108) using following command:

ping m.keyboardmag.com

its returning following results:

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

still cannot connect and download image using WebClient...

*************UPDATED CODE 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
            {
            }
Abbas
  • 4,948
  • 31
  • 95
  • 161

2 Answers2

0

Your mobile site is taking too long to respond. You are clearly hitting port 80 (as can be seen from your snippet) so if you can hit your site with a browser there isn't anything wrong with your code. You have some kind of network latency issue.

Just in case, doing:

WebClient.DownloadFile("UrlToImage");

Should work fine.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • how to check whether the site is hitting the port 80 – Abbas Dec 06 '11 at 16:51
  • You are hitting port 80, look at the ':80' after the ip address. That's the port number. Again, if you can hit the website using a browser you should be able to hit it using your code. It looks fine to me. Are you behind a proxy? Maybe you need to authenticate with the proxy before you can connect? WebClient has a property, use it of that's your case. – Icarus Dec 06 '11 at 17:13
  • @lcarus i used the authentication too, but still cannot get it to work – Abbas Dec 06 '11 at 18:00
0

Can you post some example code?

In the past I have used the code below and never had any issus.

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

Code for saving image:

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 is an int between 0-100. 50-70 is a good starting point depending on what you need.

k_man
  • 178
  • 1
  • 1
  • 7
  • hi i have uploaded the webclient code snippet i am using, can you please check the question for edits. – Abbas Dec 06 '11 at 16:35
  • hi @k_man, can you tell me whats the namespace for the class ImageResize, or its the custom class – Abbas Dec 06 '11 at 16:45
  • @Abbas, sorry about that, forgot I had that there. It is a nice helper class I found at http://www.codeproject.com/KB/GDI-plus/imageresize.aspx – k_man Dec 07 '11 at 03:51
  • @Abbas, in looking at the sample code it looks like it should work. I would try the code I have above. When I was testing I would save the originalImage to the hard drive to verify what I download was good using (see code for saving above). Also, I think url should be fully qualified with the http:// in the url. – k_man Dec 07 '11 at 04:07