【问题标题】:Resize image while uploading上传时调整图像大小
【发布时间】:2010-08-20 05:54:05
【问题描述】:

我正在使用文件上传控件来上传图像。 如果 Image.Width > 250 || 在那个我正在检查条件Image.Height > 400 然后我正在调整图像大小。 但它给出了错误 "SaveAs 方法配置为需要有根路径,并且路径 'ProductImages/roman_sandals.jpg' 没有根。"

ProductImages 是我保存图像的文件夹。 谁能找到为什么这会出错,我的代码是

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();
                }

}

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    你可以使用

    HostingEnvironment.ApplicationPhysicalPath
    

    在这种情况下,将其与您的图像路径结合起来。

    例如:

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

    这将为您提供根路径。文件夹“ProductImages”必须位于应用程序目录中。

    详情请看这里:http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.applicationphysicalpath.aspx

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多