2010-08-20 5 views
0

J'utilise le contrôle de téléchargement de fichiers pour télécharger des images. en ce que iam vérifie la condition, si Image.Width> 250 || Image.Height> 400 alors je redimensionne l'image. mais il donne l'erreur "La méthode SaveAs est configurée pour requérir un chemin enraciné, et le chemin 'ProductImages/roman_sandals.jpg' n'est pas rooté."Redimensionner l'image lors du téléchargement

ProductImages est le dossier dans lequel je sauvegarde l'image. Quelqu'un peut-il trouver pourquoi cela donne erreur, mon code est

string strBigServerPath = AppHardcodeValue.productImgPath; 
      string strFileName = ""; 
      if (prodImg.HasFile) 
      { 
       strFileName = prodImg.PostedFile.FileName; 
       string uniqueNum = Convert.ToString(System.Guid.NewGuid()); 
       string shortFileName = System.IO.Path.GetFileName(strFileName); 
       string Extension = System.IO.Path.GetExtension(prodImg.FileName); 
       string newFileName = shortFileName; 
       prodImg.SaveAs(Server.MapPath(strBigServerPath + newFileName)); 
       using (System.Drawing.Image Img = 
        System.Drawing.Image.FromFile(Server.MapPath(strBigServerPath) + newFileName)) 
       { 
        if (Img.Width > 250 || Img.Height > 400) 
        { 
         Size MainSize = new Size(250, 400); 
         using (System.Drawing.Image ImgThnail = 
           new Bitmap(Img, MainSize.Width, MainSize.Height)) 
         { 
          prodImg.SaveAs(strBigServerPath + newFileName); 
         } 
        } 
        Img.Dispose(); 
       } 
       string ThumbnailPath = Server.MapPath(AppHardcodeValue.productThumbImgPath) + newFileName; 
       using (System.Drawing.Image Img = 
        System.Drawing.Image.FromFile(Server.MapPath(strBigServerPath) + newFileName)) 
       { 
        Size ThumbNailSize = new Size(50, 50); 

        using (System.Drawing.Image ImgThnail = 
         new Bitmap(Img, ThumbNailSize.Width, ThumbNailSize.Height)) 
        { 
         ImgThnail.Save(ThumbnailPath, Img.RawFormat); 
         ImgThnail.Dispose(); 
        } 
        Img.Dispose(); 
       } 

} 

Répondre

0

Vous pouvez utiliser

HostingEnvironment.ApplicationPhysicalPath 

dans ce cas, et le combiner avec votre chemin de l'image.

Par exemple:

Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "ProductImages/roman_sandals.jpg"); 

qui vous donnera un chemin enraciné. Le dossier "ProductImages" doit être situé dans le répertoire des applications.

Voir ici pour plus de détails: http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.applicationphysicalpath.aspx

Questions connexes