Question

Dans l'un des sites mobiles, j'ai créé une page Web, dans laquelle j'utilise WebClient pour télécharger l'image depuis le site principal (site principal du mobile) et redimensionner à l'aide de bitmap, et obtenir l'image sur le site mobile, le chemin d'image pour Le site principal fonctionne bien, mais lorsque j'utilise le WebClient pour télécharger l'image pour redimensionner, j'obtiens l'erreur suivante:

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)

Quelqu'un peut-il suggérer n'importe quelle solution à ce problème que j'ai essayé de faire un ping IP ci-dessus (209.59.186.108) en utilisant la commande suivante:

ping m.keyboardmag.com

ses résultats suivants suivants:

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

ne peut toujours pas connecter et télécharger l'image à l'aide de WebClient ...

*************Extrait de code mis à jour****************

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
            {
            }
Était-ce utile?

La solution

Pouvez-vous publier un exemple de code?

Dans le passé, j'ai utilisé le code ci-dessous et je n'ai jamais eu d'ISSUS.

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

Code pour enregistrer l'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 est un int entre 0-100. 50-70 est un bon point de départ selon ce dont vous avez besoin.

Autres conseils

Votre site mobile met trop de temps à répondre. Vous frappez clairement le port 80 (comme on peut le voir à partir de votre extrait), donc si vous pouvez frapper votre site avec un navigateur, il n'y a rien de mal à votre code. Vous avez une sorte de problème de latence du réseau.

Juste au cas où, faisant:

WebClient.DownloadFile("UrlToImage");

Devrait bien fonctionner.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top