Image.ImageUrl = ...没有显示任何内容

时间:2016-12-08 06:53:36

标签: c# asp.net image

在我的开发系统(Win 10 Pro,VS 2015)上,我正在测试VS 2015中的Web表单应用程序;它使用母版页。当我尝试在调整大小后刚刚写入的文件中显示照片时,asp内容页面中的图像控件没有显示任何内容,而它显示原始照片就好了。以下是代码隐藏片段,其中包含注释中详述的问题:

    // During testing, sRelLoc contains "~/i/SlideShow/1th.jpg" (original photo)
    string sRelLocMod = sRelLoc.Replace("~/", "").Replace("/", "\\");
    // During testing, Global.sgHomeDir contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\"
    string sImgUrl = Global.sgHomeDir + sRelLocMod;
    // sImgUrl now contains 
    System.Drawing.Image Photo = null;
    // Read original photo from file
    using (FileStream fs = new FileStream(sImgUrl, FileMode.Open, FileAccess.Read))
    {
        Photo= System.Drawing.Image.FromStream(fs);
    }
    // ResizeImage from https://www.codeproject.com/articles/191424/resizing-an-image-on-the-fly-using-net
    System.Drawing.Image PhotoResized = ResizeImage(Photo, new Size(400, 400), true);
    sImgUrl = Global.sgHomeDir + "i\\tempImg.jpg";
    using (FileStream fs = new FileStream(sImgUrl, FileMode.Open, FileAccess.Write))
    {
        PhotoResized.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    // NOTE THAT: The image has been saved correctly in its resized form inside the expected directory
    sImgUrl = sImgUrl.Replace("\\", "/");
    //sImgUrl now contains "K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg"
    // Image1 is an Image control on an asp.net web page.
    // PROBLEM: The following statement displays nothing where Image1 is
    //          placed (not even a red-x or anything) , ...
    Image1.ImageUrl = sImgUrl;
    // ... but displays the unresized original just fine when the following statement is used instead:
    //          Image1.ImageUrl = sRelLoc;

相同的行为是否在调试模式下运行。

也许有一种比将大小调整后的照片写入临时文件tempImg.jpg并从中读取更好的方法?

在jignesh和Sami的回复之后更新:

我在Default.aspx.cs中使用MapPath(参见下面的新评论)。

现在使用System.Uri将路径转换为以“file:///”开头的Url。导致无法显示任何内容。用“http:”替换“file:”会导致显示损坏的图像符号。更新的代码是:

    // During testing, sRelLoc contains "~/i/SlideShow/1.jpg" (original photo)
    string sRelLocMod = sRelLoc.Replace("~/", "").Replace("/", "\\");
    // During testing, Global.sgHomeDir contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP"
    //  which was obtained in Default.aspx.cs Page Load Event by
    //    Global.sgHomeDir = HttpContext.Current.Server.MapPath(null);
    string sImgPath = Global.sgHomeDir + "\\" + sRelLocMod;
    // sImgPath now contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\i\\SlideShow\\1.jpg"
    System.Drawing.Image Photo = null;
    // Read original photo from file
    using (FileStream fs = new FileStream(sImgPath, FileMode.Open, FileAccess.Read))
    { Photo= System.Drawing.Image.FromStream(fs); }
    // ResizeImage from https://www.codeproject.com/articles/191424/resizing-an-image-on-the-fly-using-net
    System.Drawing.Image PhotoResized = ResizeImage(Photo, new Size(400, 400), true);
    sImgPath = Global.sgHomeDir + "\\i\\tempImg.jpg";
    // sImgPath now contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\i\\tempImg.jpg"
    using (FileStream fs = new FileStream(sImgPath, FileMode.OpenOrCreate, FileAccess.Write))
    { PhotoResized.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); }
    // I have verified that the image has been saved correctly in its resized form inside the expected directory
    System.Uri ImgUrl = new System.Uri(sImgPath, UriKind.Absolute);
    // Image1 is an Image control on an asp.net web page.
    string sImgUrl = ImgUrl.ToString();
    //sImgUrl now contains "file:///K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg"
    Image1.ImageUrl = sImgUrl;
    // The above statement DOES NOT work (displays nothing at all)
    // However:
    //Image1.ImageUrl = "~/i/tempImg.jpg"; // ** DOES ** work ok
    // If I replace "file:" with "http:", a broken image symbol is displayed.

但是使用代字号“〜”的工作是什么,即使它看起来不像Url。可以使用波浪号吗?是否会让我遇到麻烦,比如部署到托管服务器?

1 个答案:

答案 0 :(得分:0)

使用ImageUrl属性指定要在Image控件中显示的图像的URL。您可以使用相对或绝对URL。相对URL将图像的位置与网页的位置相关联,而不指定服务器上的完整路径。该路径相对于网页的位置。这样可以更轻松地将整个站点移动到服务器上的另一个目录,而无需更新代码。绝对URL提供完整路径,因此将站点移动到另一个目录需要您更新代码。

通过绝对URL,它们表示URL的整个IIS路径(不是您的磁盘目录路径)。 (即http://yourVirtualDirectory/ExternalImages/logo.jpg)。

所以在上面的代码中,sImgUrl =“K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg”它应该像http://yourVirtualDirectory/tempImg.jpg