我说的是这样的网站: http://www.ernesthemingwaycollection.com什么是在ASP.NET中显示随机图像的最佳方式?
它有一个静态墙纸和一组图片,从页面切换到页面,我想实现从一组显示随机图像的类似的方式使用ASP.NET的图像。
编辑:我想要的图像保持不变的会话,并从会话切换到另一个。
我说的是这样的网站: http://www.ernesthemingwaycollection.com什么是在ASP.NET中显示随机图像的最佳方式?
它有一个静态墙纸和一组图片,从页面切换到页面,我想实现从一组显示随机图像的类似的方式使用ASP.NET的图像。
编辑:我想要的图像保持不变的会话,并从会话切换到另一个。
您提到的网站没有使用随机的图像集。它们被编码到aspx页面的html一侧。
您可以在页面上放置一个asp图像控件。然后在页面的Page_Load函数中将图像设置为随机图片。
protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = "~/images/random3.jpg";
}
在存储图像集数据的位置有不同的选项。您可以使用数据库并将这些URL存储在一个表中。这将允许使用SQL中内置的Random函数。或者,您可以将XML文件保存到服务器,然后加载该文件,然后使用随机.Net类来选择一个xml节点。
就我个人而言,我会推荐数据库解决方案。
编辑:因为服务器会话20分钟后被破坏,你可能想看看使用cookie,所以你可以看到他们看到的最后一个随机图像。
如果你只是想旋转一定数量的图像,你可以使用ASP.NET AdRotator control(最后,它的用处!)。
如果你想做一些更有趣的事情,考虑使用jQuery幻灯片如jQuery Cycle Plugin。还有一个slideshow control in the AjaxControlToolkit,很容易整合。
string imageDir = "/images/banner/";
public static string chooseImage(string imageDir)
{
string[] dirs = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/" + imageDir + "/"), "*.*");
Random RandString = new Random();
string fileFullPath = dirs[RandString.Next(0, dirs.Length)];
// Do not show Thumbs.db ---
string fileName = string.Empty;
do
{
fileName = System.IO.Path.GetFileName(fileFullPath);
} while (fileName.Contains(".db"));
string imgPath = "/images/" + imageDir + "/" + fileName;
return imgPath;
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}