ASP.NET中基本的图像操作
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://ssbird.blog.51cto.com/277690/64217 |
原文地址:http://www.worldofasp.net/tut/GDI/Basic_of_GDI%20_and_Graphics_in_ASPNET_119.aspx,版权归原作者所有。 GDI+技术在Windows应用程序中应用十分广泛,深受Windows应用程序开发人员的喜爱。在.NET平台下,GDI+的某些新特性使Web开发人员同样享有这样的权利,可以在自己的Web应用程序中方便的绘制各种图形。你只需调用几个简单的Windows Api函数就可完成各种基本的图像操作。 使用GDI+在ASP.NET中绘制图形 下面这个例子演示了如何在ASP.NET中绘制矩形、三角形等简单的图形。首先新建一个.aspx文件,取名为Image.aspx,添加对 System.Drawing and System.Drawing.Imaging的引用,将下面的代码复制到Page_Load事件中。 Bitmap bmp = new Bitmap(100, 300); Graphics dc = Graphics.FromImage(bmp); Pen bluePen = new Pen(Color.Blue, 3); dc.DrawRectangle(bluePen, 0, 0, 50, 50);![]() Pen redPen = new Pen(Color.Red, 2); dc.DrawEllipse(redPen, 0, 50, 80, 60); Response.ContentType = "image/gif"; bmp.Save(Response.OutputStream, ImageFormat.Gif);在上面的代码中,绘制了一个长宽都为50的矩形和一个宽80、高50的三角形。当你在浏览器中打开这个页面时,你看到的是一个图片类型的文件。如果你在页面中添加“This is my image!”这样的文字,在浏览器中是无法显示的,原因是这个文件的输出流被设置成了ImageFormat.Gif。要解决这个问题,只需创建一个新的页面,取名Test.aspx,然后加入这样的代码: This is my image <BR> <img src="Image.aspx" /> 使用GDI+在ASP.NET中绘制字符 有些时候,我们希望网页中的某些文字以图片的形式出现,比如邮件地址等。代码如下: Image.aspx Bitmap bmp = new Bitmap(300, 30); Graphics dc = Graphics.FromImage(bmp); Font f = new Font("Verdana", 11); dc.DrawString("myemail@mydomainname.com", f, Brushes.Yellow, 0, 0); bmp.Save(Response.OutputStream, ImageFormat.Gif); Please email me at <BR /> <IMG src="Image.aspx" /> 使用GDI+生成图片验证码 有网站开发经验的人对验证码都不会陌生,这主要是用于防止用户使用机器人程序注册多个帐号。在.NET出现以前,生成验证码需要我们自己编写大量的代码,现在使用GDI+可以在ASP.NET中很方便的实现这个功能。 Random Rand = new Random(); // Create a new random number between the specified range int iNum= Rand.Next(10000, 99999); Bitmap Bmp = new Bitmap(90, 50); Graphics gfx = Graphics.FromImage(Bmp); Font fnt = new Font("Verdana", 12); // Draw the random number gfx.DrawString(RandNum.ToString(), fnt, Brushes.Yellow, 15, 15); Bmp.Save(Response.OutputStream, ImageFormat.Gif);效果图: ![]() 在上面的代码中,系统随机生成一个10000至99999的数字,并以图片格式输出到网页中。用户注册时需在文本框中输入图片中显示的字符,经过系统验证后方可完成注册。但现在的机器人程序越来越智能化,简单的验证码很容易被破解,我们需要在图片中加上一些干扰,增加机器识别的难度。 Random Rand = new Random(); int iNum = Rand.Next(10000, 99999); Bitmap Bmp = new Bitmap(90, 50); Graphics Gfx = Graphics.FromImage(Bmp); Font Fnt = new Font("Verdana", 12, FontStyle.Bold); Gfx.DrawString(iNum.ToString(), Fnt, Brushes.Yellow, 15, 15); // Create random numbers for the first line int RandY1 = Rand.Next(0, 50); int RandY2 = Rand.Next(0, 50); // Draw the first line Gfx.DrawLine(Pens.Yellow, 0, RandY1, 90, RandY2); // Create random numbers for the second line RandY1 = Rand.Next(0, 50); RandY2 = Rand.Next(0, 50); // Draw the second line Gfx.DrawLine(Pens.Yellow, 0, RandY1, 90, RandY2); Bmp.Save(Response.OutputStream, ImageFormat.Gif); Session["Number"] = iNum;效果图: ![]() .aspx文件部分的代码: <SCRIPT RUNAT=server> protected void Button1_Click(object sender, EventArgs e) { if (txtNumber.Text.Trim() == Session["Number"].ToString().Trim()) { Response.Write("Match"); } else { Response.Write("Not Match"); } } </SCRIPT> <BODY> <FORM ID="form1" RUNAT="server"> <DIV> <BR /> <IMG src="Default.aspx" /> <BR /> Please Enter the Image number <ASP:TEXTBOX ID="txtNumber" RUNAT="server"></ASP:TEXTBOX></DIV> <ASP:BUTTON ID="Button1" RUNAT="server" TEXT="Button" ONCLICK="Button1_Click" /> </FORM> </BODY>最终的运行效果: ![]() 本文出自 51CTO.COM技术博客 |
附件下载:
Download
Download


Bitmap bmp = new Bitmap(100, 300);


ssbird
博客统计信息
热门文章
最新评论
友情链接