选择显示字体大小

asp.net图象处理详解

     在使用asp的时候,我们时常要借助第三方控件来实现一些图象功能。而现在,asp.net的推出,我们已经没有必要再使用第三方控件来实现,因为asp.net 已经具有强大的功能来实现一些图象处理。现在,我们就来看看怎样使用asp.net的这一强大功能。
  
  一、system.drawing的使用
  以下的举例将演示在内存中生成一张图片,然后,将这张图片通过网页显示出来。需要了解的是,我们这里输出的不是html效果,而是实实在在的图片(图象),我们可以使用“另存为…”将输出图象保存起来。
  我们先来看看效果:
  
  我们看到,这张图片是一个渐变背景上有“看见了吗”几个字,当然,这个效果在photoshop等图象处理软件里面很容易实现,但是,一些与数据库结合的应用我们不可能将所有图片都事先设计出来,这时候,利用asp.net来实现这些功能就显得很重要了。我们来看源代码:
  <%@ page language="vb" contenttype="image/jpeg" %>
  <%@ import namespace="system.drawing" %>
  <%@ import namespace="system.drawing.imaging" %>
  <%@ import namespace="system.drawing.drawing2d" %>
  
  <%
  '清空response
  response.clear
  
  '建立一个120*30大小,24bit的bmp图象;
  dim imgoutput as new bitmap(120, 30, pixelformat.format24bpprgb)
  
  '根据以上bmp建立一个新图象;
  dim g as graphics = graphics.fromimage(imgoutput)
  
  g.clear(color.green)
  g.smoothingmode = smoothingmode.antialias
  
  g.drawstring("看见了吗?", new font("黑体",16,fontstyle.bold),new solidbrush(color.white),new pointf(2,4))
  
  g.fillrectangle(new lineargradientbrush(new point(0,0), new point(120,30), color.fromargb(0,0,0,0),color.fromargb(255,255,255,255)),0,0,120,30)
  
  imgoutput.save(response.outputstream, imageformat.jpeg)
  
  g.dispose()
  imgoutput.dispose()
  response.end
  %>
  在以上代码中,我们看到和数据库程序不同,这里专门引入了图象处理的名字空间system.drawing等。程序首先清空了response,确保没有输出;然后,程序建立了一个120乘30大的bmp图象,再在这个基础上建立一个新图象,建立图象以后,我们首先“画”出了字符串“看见了吗”,该字符串为16大粗黑体,颜色为白色,位置为(2,4);最后,我们实现渐变效果。
  以上举例很简单,但是如果和数据库结合,我们可以实现很多使用asp可能不敢想的效果。
  
  二、读取和改变图象文件大小
  读取图片?直接使用html不就可以了?当然可以,我们这里只是提供一种选择和方法来实现这一功能,具体这一功能的使用,我们可能需要在实践中更多的学习。先来看程序源代码:
  <% ' import all relevant namespaces %>
  <%@ import namespace="system" %>
  <%@ import namespace="system.drawing" %>
  <%@ import namespace="system.drawing.imaging" %>
  <%@ import namespace="system.io" %>
  
  <script runat="server">
  sub sendfile()
  dim g as system.drawing.image = system.drawing.image.fromfile(server.mappath(request("src")))
  dim thisformat=g.rawformat
  dim imgoutput as new bitmap(g, cint(request("width")), cint(request("height")))
  if thisformat.equals(system.drawing.imaging.imageformat.gif) then
  response.contenttype="image/gif"
  else
  response.contenttype="image/jpeg"
  end if
  imgoutput.save(response.outputstream, thisformat)
  g.dispose()
  imgoutput.dispose()
  end sub
  
  sub senderror()
  dim imgoutput as new bitmap(120, 120, pixelformat.format24bpprgb)
  dim g as graphics = graphics.fromimage(imgoutput)
  g.clear(color.yellow)
  g.drawstring("错误!", new font("黑体",14,fontstyle.bold),systembrushes.windowtext, new pointf(2,2))
  response.contenttype="image/gif"
  imgoutput.save(response.outputstream, imageformat.gif)
  g.dispose()
  imgoutput.dispose()
  end sub
  </script>
  
  <%
  response.clear
  if request("src")="" or request("height")="" or request("width")="" then
  call senderror()
  else
  if file.exists(server.mappath(request("src"))) then
  call sendfile()
  else
  call senderror()
  end if
  end if
  response.end
  %>
  在以上的程序中,我们看到两个函数,一个是sendfile,这一函数主要功能为显示服务器上的图片,该图片的大小通过width和height设置,同时,程序会自动检测图片类型;另外一个是senderror,这一函数的主要功能为服务器上的图片文件不存在时,显示错误信息,这里很有趣,错误信息也是通过图片给出的(如图):
  
  以上的程序显示图片并且改变图片大小,现在,我们将这个程序进一步,显示图片并且保持图片的长宽比例,这样,和实际应用可能比较接近,特别是需要制作电子相册或者是图片网站的时候比较实用。我们先来看主要函数:
  function newthumbsize(currentwidth, currentheight)
  dim tempmultiplier as double
  if currentheight > currentwidth then
  tempmultiplier = 200 / currentheight
  else
  tempmultiplier = 200 / currentwidth
  end if
  dim newsize as new size(cint(currentwidth * tempmultiplier), cint(currentheight * tempmultiplier))
  return newsize
  end function
  以上程序是增加的一个函数newthumbsize,该函数专门处理改变一会的图片大小,这个图片的长宽和原图片的长宽保持相同比例。其他部分请参考上文程序代码。
  
  三、画图特效
  如果只是将图片显示在网页上,这样未免显得简单。现在,我们来进一步感受asp.net的强大功能。我们将学习图象处理中常用的图象反转、图象切割、图象拉伸等技巧。
  先来看看程序效果:
  
  
  仔细看,我们可以找到各种图象处理效果。现在,我们来看看程序代码:
  <%@ page language="vb" debug="true" %>
  <%@ import namespace="system.drawing" %>
  <%@ import namespace="system.drawing.imaging" %>
  <%@ import namespace="system.drawing.drawing2d" %>
  <%
  dim strfilename as string
  dim i as system.drawing.image
  strfilename = server.mappath("./chris-fsck.jpg")
  
  i = system.drawing.image.fromfile(strfilename)
  
  dim b as new system.drawing.bitmap(i.width, i.height, pixelformat.format24bpprgb)
  dim g as graphics = graphics.fromimage(b)
  
  g.clear(color.blue)
  
  '旋转图片
  i.rotateflip(system.drawing.rotatefliptype.rotate90flipx)
  g.drawimage(i,new point(0,0))
  i.rotateflip(system.drawing.rotatefliptype.rotate270flipy)
  
  g.rotatetransform(10)
  g.drawimage(i,new point(0,0))
  g.rotatetransform(10)
  g.drawimage(i,new point(20,20))
  g.rotatetransform(10)
  g.drawimage(i,new point(40,40))
  g.rotatetransform(10)
  g.drawimage(i,new point(40,40))
  g.rotatetransform(-40)
  g.rotatetransform(90)
  g.drawimage(i,new rectangle(100,-400,100,50),new rectangle(20,20,i.width-20,i.height-20),graphicsunit.pixel)
  g.rotatetransform(-90)
  
  ' 拉伸图片
  g.drawimage(i,new rectangle(10,10,50,50),new rectangle(20,20,i.width-20,i.height-20),graphicsunit.pixel)
  g.drawimage(i,new rectangle(50,10,90,50),new rectangle(20,20,i.width-20,i.height-20),graphicsunit.pixel)
  g.drawimage(i,new rectangle(110,10,150,50),new rectangle(20,20,i.width-20,i.height-20),graphicsunit.pixel)
  
  
  '切割图片
  g.drawimage(i,50,100,new rectangle(180,80,60,110),graphicsunit.pixel)
  g.drawimage(i,140,100,new rectangle(180,80,60,110),graphicsunit.pixel)
  
  '旋转图片
  i.rotateflip(system.drawing.rotatefliptype.rotate180flipx)
  g.drawimage(i,230,100,new rectangle(180,110,60,110),graphicsunit.pixel)
  
  response.contenttype="image/jpeg"
  
  b.save(response.outputstream, imageformat.jpeg)
  
  b.dispose()
  
  %>
  在以上的程序中,我们看到实现图象处理的各种技巧,仔细观察,我们可以知道旋转图片其实是用了一个rotateflip方法;而切割和拉伸图片,完全是通过设置drawimage的不同参数来实现。
  
  四、总结
  asp.net的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要我们在实践中摸索、总结。
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons