首页 > Web开发 > 详细

Asp.net从文件夹中读取图片,随机背景图

时间:2014-03-31 12:59:44      阅读:691      评论:0      收藏:0      [点我收藏+]

第一步:配置文件web.config里添加

<system.web>
<connectionStrings>

<!--name 是自定义的,connectionString 的值是你存放图片的虚拟路径-->
<add name="strCon" connectionString="~/Image"/>
</connectionStrings>
</system.web>

第二步:aspx页面
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style>
body {
background-image:url("<%=Url %>")
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div >
<%--<img src="<%=Url %>"/>--%>
</div>
</form>
</body>
</html>

 

第三步:aspx.cs代码

 

public partial class Background : System.Web.UI.Page
{
protected string Url = "";
protected void Page_Load(object sender, EventArgs e)
{
Url = Test();
}
//获取图片路径
public string Test()
{
//配置文件里配置虚拟路径引用Configuration
string filepath =this.Server.MapPath(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);

//引用IO
string[] files= Directory.GetFiles(filepath);

if (files.Length > 0)
{

//随机读取
Random rand = new Random();
int num= rand.Next(files.Count());
FileInfo file = new FileInfo(files[num]);
if (file.Extension == ".jpg" || file.Extension == "gif")
{
return "/Image/" + file.Name;
}

}
return "/images/1.jpg";
bubuko.com,布布扣
}
}

Asp.net从文件夹中读取图片,随机背景图,布布扣,bubuko.com

Asp.net从文件夹中读取图片,随机背景图

原文:http://www.cnblogs.com/youchim/p/3633669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!