随机图片

本文介绍了一个使用C#编写的ASP.NET应用程序,该程序每两秒自动刷新一次以显示带有随机位置图标的图像。通过生成随机数确定图标的位置,并将最终合成的图像保存到服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

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;
}
}

 

转载于:https://www.cnblogs.com/jRoger/articles/1833513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值