随机图片
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FreshImg.aspx.cs" Inherits="FreshImg" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>随机图像</title>
<!--每2秒刷新-->
<meta http-equiv="refresh" content="2" />
</head>
<body>
<form id="form1" runat="server" >
<div>
<img ID="DTImg" runat="server" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FreshImg.aspx.cs" Inherits="FreshImg" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>随机图像</title>
<!--每2秒刷新-->
<meta http-equiv="refresh" content="2" />
</head>
<body>
<form id="form1" runat="server" >
<div>
<img ID="DTImg" runat="server" />
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
public partial class FreshImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DTImg.Src = CreateImage();
}
public string CreateImage()
{
//创建Graphics对象
Bitmap backimg = new Bitmap(800, 600);
Graphics g = Graphics.FromImage(backimg);
//通过产生随机数来确定随机位置
int x = 0, y = 0;
Random r = new Random();
g.Clear(Color.White);
//显示各图标
Bitmap dyimg = new Bitmap(Server.MapPath("sampledi.gif"));
for (int i = 0; i < 5; i++)
g.DrawImage(dyimg, x + r.Next(300), y + r.Next(300));
//存储页面图像
string imgpath = Server.MapPath("sample.gif");
backimg.Save(imgpath, System.Drawing.Imaging.ImageFormat.Gif);
return imgpath;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
public partial class FreshImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DTImg.Src = CreateImage();
}
public string CreateImage()
{
//创建Graphics对象
Bitmap backimg = new Bitmap(800, 600);
Graphics g = Graphics.FromImage(backimg);
//通过产生随机数来确定随机位置
int x = 0, y = 0;
Random r = new Random();
g.Clear(Color.White);
//显示各图标
Bitmap dyimg = new Bitmap(Server.MapPath("sampledi.gif"));
for (int i = 0; i < 5; i++)
g.DrawImage(dyimg, x + r.Next(300), y + r.Next(300));
//存储页面图像
string imgpath = Server.MapPath("sample.gif");
backimg.Save(imgpath, System.Drawing.Imaging.ImageFormat.Gif);
return imgpath;
}
}