积极答复者
帮忙看看偶书上的代码,偶不懂.

问题
-
Timer_gdiplus.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Timer_GdiPlus.aspx.cs" Inherits="Timer_GdiPlus" %>
<%--<%@ Register Assembly="System.Web.Extensions, Version=2.0.50727, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %> --%>
<%--
<%@ Register Assembly="System.Web.Extensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %> --%><!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<img id="imgTime" src="" runat="server" alt="a" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
</form>
</body>
</html>
timer_gdiplus.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;public partial class Timer_GdiPlus : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void Timer1_Tick(object sender, EventArgs e)
{
imgTime.Visible = true;
imgTime.Src = "ImageHandler.ashx?" + DateTime.Now.Second; //1.这句话似懂非懂,给imgTime.Src赋值,那么DateTime.Now.Second又是干什么呢?“ImageHandler.ashx?" +“17:40:56”变成"ImageHandler.ashx?17:40:56"又是怎么起作用,怎样的流程呢?谢谢,这是第一个问题
}
}
ImageHandler.ashx内容
<%@ WebHandler Language="C#" Class="ImageHandler" %>using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) //2.这里的context是输入还是输出呢?
{
Bitmap imageTime = new Bitmap(500, 100);
Graphics g = Graphics.FromImage(imageTime);
SolidBrush colorPen = new SolidBrush(Color.White);
g.FillRectangle(colorPen, 0, 0, 500, 800);
Array obj = Enum.GetValues(typeof(HatchStyle));
int valueStyle = new Random().Next(obj.Length);
HatchStyle brushStyle = (HatchStyle)obj.GetValue(valueStyle);
HatchBrush theBrush = new HatchBrush(brushStyle, Color.White, Color.Black);
g.DrawString(DateTime.Now.ToLongTimeString(), new Font("Arial Black", 48), theBrush, 0, 0);
MemoryStream ms = new MemoryStream();
imageTime.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin); //3.这个Seek做啥用呢?
ms.Read(buffer, 0, (int)ms.Length);
ms.Close();
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();buffer = null;
imageTime.Dispose();
g.Dispose();
}
public bool IsReusable //4.这段代码干什么呢?
{
get
{
return false;
}
}
}
maiyude
答案
-
你好,你的第一个问题,我想主要是为了避免缓存的问题,因为IE浏览器会缓存图片,如果不加一些随时变动的字符串到url中那么就无法读取最新的,在验证码等很多方面都是这样运用的DateTime.Now.Second则是获取当前时间的秒的部分 第二个问题,HttpContext是封装有关个别 HTTP请求的所有HTTP 特定的信息,它包含了Request属性则是当前请求的相关信息即HttpRequest的实例,它的Response属性则是用于对当前请求的响应 第三个问题,Seek方法则是将当前流中的位置设置为指定值,SeekOrigin.Begin则是指定流的开头 ms.Seek(0, SeekOrigin.Begin); 则是将当前位置设置到流的开始处 ms.Read(buffer, 0, (int)ms.Length); 则是读取流中所有的字节 第四个问题,它是IHttpHandler.IsReusable,它设定个值指示其他请求是否可以使用IHttpHandler的这个实例
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond- 已建议为答案 mldark 2009年8月23日 8:52
- 已标记为答案 邹俊才Moderator 2009年8月24日 14:40