【问题标题】:Image.ImageUrl = ... nothing is displayedImage.ImageUrl = ... 什么都不显示
【发布时间】:2016-12-09 03:43:06
【问题描述】:

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

    // 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(请参阅下面的新 cmets)。

现在使用 System.Uri 将路径转换为以“file:///”开头的 Url。导致什么都没有显示。将“file:”替换为“http:”会导致显示损坏的图像符号。更新后的代码是:

    // 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。可以使用波浪号吗?是否会给我带来任何麻烦,例如部署到托管服务器时?

【问题讨论】:

  • 因为浏览器无法访问您的磁盘。您必须给它一个 URL,而不是路径。也不要手动更改 URL 到路径,这就是 MapPath() 的用途。

标签: c# asp.net image


【解决方案1】:

使用 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

【讨论】:

    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 2018-06-29
    • 1970-01-01
    相关资源
    最近更新 更多