(1)直接从文件夹中读取
string dirpath = Server.MapPath("Emoticons");
DirectoryInfo dire = new DirectoryInfo(dirpath);
FileInfo[] file1 = dire.GetFiles("*.gif");
foreach (FileInfo f in file1)
{
ListItem item1 = new ListItem("<img src='" + "Emoticons/" + f.Name + "'/>", "Emoticons/" + f.Name);
radImageFace.Items.Add(item1); //RadioButtonList 控件
}
FileInfo[] file2 = dire.GetFiles("*.jpg");
foreach (FileInfo f in file2)
{
ListItem item1 = new ListItem("<img src='" + "Emoticons/" + f.Name + "'/>", "Emoticons/" + f.Name);
radImageFace.Items.Add(item1);
}
(2)与数据库数据绑定
就是在绑定时设置DataValueField为一个图片就可以了,DataValueField="<img src="Url.gif">";。例:在数据库的Image表中字段Url是存图片的地址。则
sql语句为:select '<img src=' + Url + '>' as ImageUrl,ID from Image;
string connectionString =@"server=TENCENT-A1AB25B\OG2000;database=blogs;uid=sa;pwd=sa";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter Adapter = new SqlDataAdapter("select '<img src=' + name + '>' as ImageUrl,ID from Image", connection);
DataSet DS = new DataSet();
Adapter.Fill(DS, "表名");
radImageFace.DataSource = DS;
radImageFace.DataTextField = "ImageUrl";
radImageFace.DataValueField = "ID";
radImageFace.DataBind();