创建基于AJAX技术的Scribble应用程序

http://tech.ddvip.com   2006年11月20日    社区交流

本文详细介绍创建基于AJAX技术的Scribble应用程序

  7. 在"Add Reference"对话框中选择"System.Drawing"并且点击OK。

  8. 最后,在"Build"菜单下点击"Build Web Site"或按组合键Ctrl+Shift+B来确保没有构建错误。

ScribbleImage.ashx

  这个web处理器用于把存储在会话变量中的图像数据流回客户端。

  1. 在"WebSite"菜单中点击"Add New Item"或按下Ctrl+Shift+A。

  2. 在"Add New Item"对话框中选择"Generic Handler",然后把该处理器的名字设置为ScribbleImage.ashx并且点击OK。

  3. 一个web处理器要使用会话变量,它需要实现接口IRequiresSessionState。这是唯一的标记接口并且没有要重载的方法。如下所示编辑该类的声明:

public class ScribbleImage : IHttpHandler,
System.Web.SessionState.IRequiresSessionState

  4. 接下来,我们把代码添加到ProcessRequest方法。

public void ProcessRequest (HttpContext context){
 context.Response.ContentType = "image/png";
 context.Response.Cache.SetNoStore();
 context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
 context.Response.Cache.SetExpires(DateTime.Now);
 context.Response.Cache.SetValidUntilExpires(false);
 System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)context.Session["Image"];
 lock(bmp)
 {
  using (MemoryStream ms = new MemoryStream())
  {
   bmp.Save(ms, ImageFormat.Png);
   ms.Flush();
   context.Response.BinaryWrite(ms.GetBuffer());
  }
 }
 }
}

  o 第一行设置相应于image/png的响应的ContentType头。这确保浏览器认出该响应是一个png图像而不是普通HTML代码。

来源:天极开发    作者:朱先忠    责编:豆豆技术应用

正在加载评论...