开发者

Can we use Server.MapPath in Bitmap?

recently i created a webpage, where in i have a img tag, whose source is linked to another page, where i am resizing the image, whose name is being sent from the src from previous page in query string. but when i create the new object of bitmap, i gets the error, Parameter is not valid.

below is the code which holds image tag.

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

below is the code for the resize page where i am resizing image and sending the bitmap object through response

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
            {
            }
        }

the above code works fine in local Machine on FileSystem, but when i put the same code on dev server, the application starts throwing message..

can anyone tell me what could be the cause for this problem only on dev server.


If you don't specify a root folder for Server.MapPath it will add the location of the currently executing aspx file. You can read more at 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

As Hanlet mentioned you need to add an images root folder. So your code will become

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

    loBMP = new Bitmap(Server.MapPath(imgRoot + imgUrl));
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜