使用c#ASP.NET加载页面时获取FileNotFoundException

时间:2015-07-14 07:27:04

标签: c# asp.net

我想使用c#ASP.NET在我的网站上显示我的横幅图片。当我的页面加载时,它会抛出以下错误。

错误:

/Upload/Banner/2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: /Upload/Banner/2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg

Source Error: 


Line 88:         {
Line 89:             Bitmap newImage = new Bitmap(newWidth, newHeight);
Line 90:             System.Drawing.Image srcImage = System.Drawing.Image.FromFile(imageDirectory);
Line 91:             using (Graphics gr = Graphics.FromImage(newImage))
Line 92:             {

我正在解释下面的代码。

  

的Index.aspx:

<section class="slider" id="slider">
                 <asp:Repeater ID="rptBannerId" runat="server">
                 <ItemTemplate>
                    <div class="ls-slide" data-ls="<%# getLs(Container.ItemIndex) %>">
                   <img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64("/Upload/Banner/" + Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />

                        <div class="intro ls-l" data-ls="<%# getDataLs(Container.ItemIndex) %>" style="left:80%;top:35%;">
                            <span class="icon fa fa-heart"></span>
                            <h2><span>"<%# getSpanValue(Container.ItemIndex)%>"</span>"<%# getH2Value(Container.ItemIndex)%>"</h2>
                            <p><%# Eval("Bnr_Description")%></p>
                            <div class="buttons">
                                <a href="" class="prev"><i class="fa fa-angle-left"></i></a>
                                <a href="" class="button">Read More</a>
                                <a href="" class="next"><i class="fa fa-angle-right"></i></a>
                            </div>
                        </div>
              </div>
              </ItemTemplate>
              </asp:Repeater> 
                </section>
  

index.aspx.cs:

protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
        {
            Bitmap newImage = new Bitmap(newWidth, newHeight);
            System.Drawing.Image srcImage = System.Drawing.Image.FromFile(imageDirectory);
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode = SmoothingMode.HighQuality;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));

            }
            MemoryStream ms = new MemoryStream();
            newImage.Save(ms, ImageFormat.Gif);
            var base64Data = Convert.ToBase64String(ms.ToArray());
            return "data:image/gif;base64," + base64Data;
        }

实际上所有图像都存储在此C:/ASP project/Odiya_Doctor_Client/Odiya_Doctor_Client/Upload/Banner/路径中。我的要求是如果我直接插入此路径,则此错误不会出现但我需要用户将使用任何其他驱动器并放置此项目({{ 1}})图像应该显示,所以我让它变得动态。请帮我解决这个错误。

1 个答案:

答案 0 :(得分:0)

您应该使用Server.MapPath

像: (Server.MapPath(“/ Upload / Banner /”)+ Convert.ToString(Eval(“Bnr_Image”))

根据您的应用程序更改文件夹序列。以下是Server.MapPath

的不同选项

Server.MapPath指定映射到物理目录的相对或虚拟路径。

Server.MapPath(“。”)1返回正在执行的文件(例如aspx)的当前物理目录 Server.MapPath(“..”)返回父目录 Server.MapPath(“〜”)返回应用程序根目录的物理路径 Server.MapPath(“/”)返回域名根目录的物理路径(不一定与应用程序的根目录相同)