Image控件當(dāng)沒有圖片時(shí),顯示"紅色的X" 不夠友好,可以設(shè)置其AlternateText屬性,也可以顯示指定的圖片
1.普通顯示
string str ="~/img/"+int.Parse(TextBox1.Text)+".gif";
if (!File.Exists(Server.MapPath(str)))//不存在時(shí)顯示nophoto.gif
{
Image1.ImageUrl = "~/img/nophoto.gif";
//Response.Write("bucunzai");
}
else//有的時(shí)候顯示
{
Image1.ImageUrl = str;
//Response.Write("cunzai");
}
2.還有一種可能是從數(shù)據(jù)庫中讀取二進(jìn)制的圖片時(shí)(紅色部分需要從數(shù)據(jù)庫中讀取)
byte [] b=....;//從數(shù)據(jù)庫中讀取
if (b == null || b.Length == 0)
{
//當(dāng)沒有圖片數(shù)據(jù)時(shí)顯示默認(rèn)的圖片nophoto.gif
FileStream fs = newFileStream(Server.MapPath("~/Management/Images/nophoto.gif"),FileMode.Open, FileAccess.Read);
byte[] mydata = new byte[fs.Length];
int Length = Convert.ToInt32(fs.Length);
fs.Read(mydata, 0, Length);
fs.Close();
this.Response.OutputStream.Write(mydata, 0, Length);
this.Response.End();
}
else
{
Response.ContentType = ...;//從數(shù)據(jù)庫中讀取圖片的后綴名
Response.OutputStream.Write(b, 0, b.Length);
}