C# 图像未显示在浏览器中

C# 图像未显示在浏览器中,c#,asp.net,visual-studio-2012,C#,Asp.net,Visual Studio 2012,嘿,伙计们,我正在为我的未婚夫制作一个艺术网站,但不能让图片显示出来,我做的和我制作的预览网站完全一样,但从2008年开始使用visual studio 2012。这是代码。我在图像中得到了错误符号,而不是正确的图像。它从数据库加载路径,该数据库为Art/1.jpg using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using Syst

嘿,伙计们,我正在为我的未婚夫制作一个艺术网站,但不能让图片显示出来,我做的和我制作的预览网站完全一样,但从2008年开始使用visual studio 2012。这是代码。我在图像中得到了错误符号,而不是正确的图像。它从数据库加载路径,该数据库为Art/1.jpg

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Home : System.Web.UI.Page
{
int Add = 1;

protected void Page_Init(object sender, EventArgs e)
{
        Database s = new Database();
        int Total = s.ArtCount();                          //Count the total of cars int the database
        for (int loop = Total; loop > 0; loop--)
        {
            Panel Panel1 = new Panel();                     //Create a new panel for all the cars
            Panel1.ID = "pnl" + loop.ToString();
            Panel1.Style["position"] = "absolute";
            Panel1.Style["left"] = "155px";
            Panel1.Style["top"] = "400px";
            Panel1.Style["Height"] = "200px";
            Panel1.Style["Width"] = "1000px";
            Panel1.BackColor = System.Drawing.Color.Black;
            form1.Controls.Add(Panel1);

            if (Add >= 2)
            {
                int Top = (400 * (Add - 1));
                Panel1.Style["top"] = (250 + Top + 20).ToString() + "px";       //Set the second and more panels down
            }
            Add++;

            string Art = s.LoadArt(loop);
            string ArtID = Art.Substring(0, Art.IndexOf("|"));
            Art = Art.Substring(ArtID.Length + 1);

            string ArtName = Art.Substring(0, Art.IndexOf("|"));
            Art = Art.Substring(ArtName.Length + 1);

            string Description = Art.Substring(0, Art.IndexOf("|"));
            Art = Art.Substring(Description.Length + 1);

            string PicUrl = Art.Substring(0, Art.IndexOf("|"));
            string path = Server.MapPath(PicUrl);

            Image image1 = new Image();
            image1.ID = "img" + ArtID +loop;
            image1.ImageAlign = ImageAlign.AbsMiddle;
            image1.Visible = true;            
            image1.ImageUrl = path;
            image1.Style["Left"] = "10px";
            image1.Style["position"] = "absolute";
            image1.Style["top"] = "10px";
            image1.Height = 180;
            image1.Width = 230;
            Panel1.Controls.Add(image1);


            Label label1 = new Label();                                 //Create a label for the description of each car
            label1.ID = "lblDescription" + ArtID;
            label1.Text = Description;
            label1.ForeColor = System.Drawing.Color.Lime;
            label1.BorderStyle = BorderStyle.Groove;
            label1.BorderColor = System.Drawing.Color.Violet;
            label1.Style["Left"] = "500px";
            label1.Style["position"] = "absolute";
            label1.Style["top"] = "30px";
            Panel1.Controls.Add(label1);

            string View = s.TimesView(ArtID);
            Label label10 = new Label();                                //Create a label for the times each car is viewed
            label10.ID = "lblView" + ArtID+loop;
            label10.Text = "Times Viewed: " + View;
            label10.ForeColor = System.Drawing.Color.Lime;
            label10.Style["Left"] = "799px";
            label10.Style["position"] = "absolute";
            label10.Style["top"] = "170px";
            Panel1.Controls.Add(label10);

            Button btnView = new Button();                              //Create a button to view a car for all cars
            btnView.ID = ArtID;
            btnView.Text = "View";
            btnView.ForeColor = System.Drawing.Color.DeepSkyBlue;
            btnView.BackColor = System.Drawing.Color.Gray;
            btnView.BorderColor = System.Drawing.Color.Violet;
            btnView.Style["top"] = "150px";
            btnView.Style["left"] = "800px";
            btnView.Style["Height"] = "20px";
            btnView.Style["position"] = "absolute";
            btnView.BorderStyle = BorderStyle.Outset;
            btnView.Command += new CommandEventHandler(btnView_Command);    //Create a command EventHandler to now what button was clicked
            btnView.CommandArgument = ArtID;                                //Set the commandArguments = to the carID
            Panel1.Controls.Add(btnView);
        }
    }

void btnView_Command(object sender, CommandEventArgs e)
{
    string ArtID = e.CommandArgument.ToString();                                    //Gets theCarId from the CommandArgument
    Session["ArtID"] = ArtID;                                                       //Create session CarID and set it = to the CarID
    Response.Redirect("ViewArt.aspx");                                              //Redirect to the ViewCar page
}
}

艺术文件夹是网页根目录的子文件夹还是其他地方的子文件夹?我怀疑,虽然路径正确地从数据库返回,但路径艺术不是当前路径的子路径。正如您所知,Server.MapPath将返回物理路径。使用Pig.SealviURL或Page.ResoveClientUrl来给你绝对URL,也可以考虑使用/学习CSS。使用Style属性以及ForeColor和其他样式修改属性将在生成的html中生成内联样式标记。这不仅丑陋,而且浪费带宽。