0

最近我创建了一个网页,其中我有一个 img 标签,其源链接到另一个页面,我正在调整图像大小,其名称是从查询字符串中的上一页的 src 发送的。但是当我创建位图的新对象时,我得到了错误,参数无效。

下面是包含图像标签的代码。

 <img src='/resize.aspx?file=PRO_06_11_Final-272.jpg&width=128&height=73' alt="Nothing" />

下面是调整大小页面的代码,我正在调整图像大小并通过响应发送位图对象

if (Request.QueryString["file"] != null)
        {

            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;
                loBMP = new Bitmap(Server.MapPath(imgUrl)); //Parameter is not valid.. error is thrown here.
                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;
                    }
                 }
                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);
                Response.ContentType = "image/jpeg";
                bmpOut.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write("CreateThumbnail :" + ex.ToString());
            }
            finally
            {
            }
        }

上面的代码在文件系统上的本地机器上工作正常,但是当我把相同的代码放在开发服务器上时,应用程序开始抛出消息..

谁能告诉我仅在开发服务器上导致此问题的原因是什么。

4

1 回答 1

1

如果您不指定根文件夹,Server.MapPath它将添加当前执行的 aspx 文件的位置。您可以在msdn上阅读更多内容

If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed

正如 Hanlet 提到的,您需要添加一个图像根文件夹。所以你的代码会变成

string imgRoot = "~/images/";
try
{
    ...

    loBMP = new Bitmap(Server.MapPath(imgRoot + imgUrl));
    ...
}
于 2011-10-03T14:49:02.317 回答