惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
K
Kaspersky official blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Recorded Future
Recorded Future
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
S
Schneier on Security
P
Palo Alto Networks Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
N
Netflix TechBlog - Medium
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
C
Check Point Blog
L
LangChain Blog
腾讯CDC
小众软件
小众软件
T
Tenable Blog
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
About on SuperTechFans
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
雷峰网
雷峰网
美团技术团队
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
博客园_首页

博客园 - 叶漂

用于样式检测的临时日志(e1c2f313-db73-47f6-8dd1-333bb19f153f - 3bfe001a-32de-4114-a6b4-4005b770f6d7) fckeditor的几个问题的解决 大家放松下吧,咱家先在首页呆会儿! - 叶漂 - 博客园 你真的懂我吗?(谈谈C#接口)之一 Visual Studio 2005插件开发 Visual Studio 2005插件开发(代码行数统计器)之二 Visual Studio 2005 插件编程(代码行数统计插件)之一 Visual Studio 2005 插件编程(代码行数统计插件) VS2003中基于Word2003编程 DI(依赖注入) 用Visual Studio 2005 开发VB.NET-C#语言转换插件 控件适配器开启基本控件的控件状态 控件状态VS视图状态 ViewState(视图状态)持久性机制大全! 暂时放在首页,因为需要大家帮忙!! 给大家介绍一个好工具:SQL智能完成工具! Atlas指南: 建立一个AJAX 涂鸦程序(三) Atlas指南: 建立一个AJAX 涂鸦程序(一) 资源与本地化(三)
Atlas指南: 建立一个AJAX 涂鸦程序(二)
叶漂 · 2006-05-23 · via 博客园 - 叶漂

原文地址:http://www.codeproject.com/aspnet/ajax_scribble.asp
源代码:http://www.codeproject.com/aspnet/ajax_scribble/ajax_scribble_src.zip

Atlas指南: 建立一个AJAX 涂鸦程序(二)

Global.asax

我们从Global.asax开始我们的编码过程。

1.      Website 菜单 点击 Add New Item 或者 Ctrl + Shift + A.

2.      Add New Item 对话框选择Global Application Class 并点击OK.你就可以看到已经创建了一个Global.asax文件了。

3.      导入 System.Drawing 名称空间. 在第一行下插入如下的代码。

<%@ Import Namespace="System.Drawing" %>        

4.      Session_Start 方法中增加如下代码.

void Session_Start(object sender, EventArgs e)

{

    Bitmap bmp = new Bitmap(200, 200);

    using (Graphics g = Graphics.FromImage(bmp))

    {

        g.FillRectangle(new SolidBrush(Color.White),

            new Rectangle(0, 0, bmp.Width, bmp.Height));

        g.Flush();

    }

    Session["Image"] = bmp;

}       

这代码建立一个简单的bitmap 200*200像素白色,把整个背景绘制成白色的位图,并把它存储在名称为Image 的变量中。

5. Session_End 函数应该销毁存储在Session中的Image对象。

 Bitmap bmp = (Bitmap)Session["Image"];

 bmp.Dispose();

6. Website 菜单选择 Add Reference.

7. 选择 System.Drawing Add Reference 对话框并点击OK

8. 最后,在Build 菜单点击Build Web Site 或者按下 Ctrl + Shift + B 确保 没有任何编译错误。

ScribbleImage.ashx

web处理器假定把存储在session中的image对象流回到客户端。

  1. WebSite 菜单点击 Add New Item 或者按下 Ctrl + Shift + A.
  2. Add New Item对话框选择Generic Handler, handler的名称改为ScribbleImage.ashx 并点击OK.
  3. 为了让一个web handler使用session变量,它需要实现接口IRequiresSessionState. 这只是一个标记性接口,没有任何方法。

public class ScribbleImage : IHttpHandler,

    System.Web.SessionState.IRequiresSessionState

  1. 接下来我们向 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());

        }

    }

    }

}

    • 第一行设置 ContentType header in the response to image/png. 这确保浏览器识别响应是一个png image而不是HTML.
    • 接下来的四行指示浏览器,该响应不需要缓存。这四行是必需的,保证了代码跨浏览器兼容。我们将在指南的后续版本中优化这些代码。
    • 最后,session变量中来的bitmap被保存在内存流中(memory stream),并且内存流中的内容被写向响应。BinaryWrite 函数被使用,因为image是二进制数据。

ScribbleService.asmx

我们已经对初始化session image和把image内容流向响应(response)有一定了解了,现在我们需要某个方法把内容加入image对象自己。我们期望客户端调用ScribbleService.asmx web服务把lines加入image

  1. WebSite 菜单点击Add New Item 或者按下Ctrl + Shift + A.
  2. Add New Item对话框选择Web Service, 确认名称为ScribbleService.asmx 并点击OK.确保你没有选择Place Code in a Separate File.
  3. 导入名称空间 System.Drawing

using System.Drawing;

4. 接下来,我们需要为point定义一个简单的类。我们不能使用System.Drawing.Point 类因为它不能XML序列化。在以后的指南中我们将看到如何用System.Drawing.Point 代替自定义的类。在定义ScribbleService 类之前,我们加入下面的代码:

public class Point

{

   public int X;

   public int Y;

}

  5. 最后, 我们需要增加一个方法来根据给定的一些点绘制草图。我们增一个Draw web 方法:  [WebMethod(EnableSession = true)]

public void Draw(Point[] points)

{

    Image scribbleImage = (Image)Session["Image"];

    lock(scribbleImage)

    {

        using (Graphics g = Graphics.FromImage(scribbleImage))

        using(Pen p = new Pen(Color.Black, 2))

        {

            if (points.Length > 1)

            {

                int startX = points[0].X;

                int startY = points[0].Y;

                for (long i = 1; i < points.Length; i++)

                {

                    g.DrawLine(p, startX, startY,

                        points[i].X, points[i].Y);

                    startX = points[i].X;

                    startY = points[i].Y;

                }

            }

        }

    }

}

    • 特性(attribute WebMethod(EnableSession = true) 确保从web服务能访问session变量。
    • Image被锁定确保并发访问是安全的
    • 绘制它自己是相当简单的,就是把points 数组中的点连接起来.


本想一次发完,但就是发不成功,郁闷!! 未完!!