随风设计

随风设计

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  11 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
  1 ASP.NET图像处理详解
  2    在使用ASP的时候,我们时常要借助第三方控件来实现一些图象功能。而现在,ASP.NET的推出,我们已经没有必要再使用第三方控件来实现,因为ASP.NET 已经具有强大的功能来实现一些图象处理。现在,我们就来看看怎样使用ASP.NET的这一强大功能。 
  3
  4    一、System.Drawing的使用 
  5
  6    以下的举例将演示在内存中生成一张图片,然后,将这张图片通过网页显示出来。需要了解的是,我们这里输出的不是HTML效果,而是实实在在的图片(图象),我们可以使用“另存为…”将输出图象保存起来。 
  7
  8    我们先来看看效果: 
  9
 10  
 11    我们看到,这张图片是一个渐变背景上有“看见了吗”几个字,当然,这个效果在PhotoShop等图象处理软件里面很容易实现,但是,一些与数据库结合的应用我们不可能将所有图片都事先设计出来,这时候,利用ASP.NET来实现这些功能就显得很重要了。我们来看源代码: 
 12
 13 <%@ page language = " vb "  contenttype = " image/jpeg "  %> 
 14 <%@ import  namespace = " system.drawing "  %> 
 15 <%@ import  namespace = " system.drawing.imaging "  %> 
 16 <%@ import  namespace = " system.drawing.drawing2d "  %> 
 17
 18 <% 
 19 ' 清空Response 
 20 response.clear 
 21
 22 ' 建立一个120*30大小,24bit的BMP图象; 
 23 dim  imgOutput  as   New  bitmap( 120 30 , pixelformat.format24bpprgb) 
 24
 25 ' 根据以上BMP建立一个新图象; 
 26 dim  g  as  graphics  =  graphics.fromimage(imgOutput) 
 27
 28 g.clear(color.Green) 
 29 g.smoothingMode  =  smoothingMode.antiAlias 
 30
 31 g.drawString( " 看见了吗? " New  font( " 黑体 " , 16 ,fontstyle.bold), new  SolidBrush(Color.White), New  pointF( 2 , 4 )) 
 32
 33 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
 34
 35 imgOutput.save(response.outputstream, imageformat.jpeg) 
 36
 37 g.dispose() 
 38 imgOutput.dispose() 
 39 response.end 
 40 %> 
 41
 42
 43    在以上代码中,我们看到和数据库程序不同,这里专门引入了图象处理的名字空间system.drawing等。程序首先清空了Response,确保没有输出;然后,程序建立了一个120乘30大的BMP图象,再在这个基础上建立一个新图象,建立图象以后,我们首先“画”出了字符串“看见了吗”,该字符串为16大粗黑体,颜色为白色,位置为( 2 4 );最后,我们实现渐变效果。 
 44
 45    以上举例很简单,但是如果和数据库结合,我们可以实现很多使用ASP可能不敢想的效果。 
 46 二、读取和改变图象文件大小 
 47
 48    读取图片?直接使用HTML不就可以了?当然可以,我们这里只是提供一种选择和方法来实现这一功能,具体这一功能的使用,我们可能需要在实践中更多的学习。先来看程序源代码: 
 49
 50 <%  '  import all relevant namespaces %> 
 51 <%@ import  namespace = " System "  %> 
 52 <%@ import  namespace = " System.Drawing "  %> 
 53 <%@ import  namespace = " System.Drawing.Imaging "  %> 
 54 <%@ import  namespace = " System.IO "  %> 
 55
 56 <script runat = " server " > 
 57 Sub  sendFile() 
 58 dim  g  as  System.Drawing.Image  =  System.Drawing.Image.FromFile(server.mappath(request( " src " ))) 
 59 dim  thisFormat = g.rawformat 
 60 dim  imgOutput  as   New  Bitmap(g,  cint (request( " width " )),  cint (request( " height " ))) 
 61 if  thisformat.equals(system.drawing.imaging.imageformat.Gif)  then  
 62 response.contenttype = " image/gif "  
 63 else  
 64 response.contenttype = " image/jpeg "  
 65 end   if  
 66 imgOutput.save(response.outputstream, thisformat) 
 67 g.dispose() 
 68 imgOutput.dispose() 
 69 end sub
 
 70
 71 Sub  sendError() 
 72 dim  imgOutput  as   New  bitmap( 120 120 , pixelformat.format24bpprgb) 
 73 dim  g  as  graphics  =  graphics.fromimage(imgOutput) 
 74 g.clear(color.yellow) 
 75 g.drawString( " 错误! " New  font( " 黑体 " , 14 ,fontstyle.bold),systembrushes.windowtext,  New  pointF( 2 , 2 )) 
 76 response.contenttype = " image/gif "  
 77 imgOutput.save(response.outputstream, imageformat.gif) 
 78 g.dispose() 
 79 imgOutput.dispose() 
 80 end sub
 
 81 / script> 
 82
 83 <% 
 84 response.clear 
 85 if  request( " src " ) = ""   or  request( " height " ) = ""   or  request( " width " ) = ""   then  
 86 call  sendError() 
 87 else  
 88 if  file.exists(server.mappath(request( " src " )))  then  
 89 call  sendFile() 
 90 else  
 91 call  sendError() 
 92 end   if  
 93 end   if  
 94 response.end 
 95 %> 
 96
 97    在以上的程序中,我们看到两个函数,一个是SendFile,这一函数主要功能为显示服务器上的图片,该图片的大小通过Width和Height设置,同时,程序会自动检测图片类型;另外一个是SendError,这一函数的主要功能为服务器上的图片文件不存在时,显示错误信息,这里很有趣,错误信息也是通过图片给出的(如图): 
 98
 99  
100 以上的程序显示图片并且改变图片大小,现在,我们将这个程序进一步,显示图片并且保持图片的长宽比例,这样,和实际应用可能比较接近,特别是需要制作电子相册或者是图片网站的时候比较实用。我们先来看主要函数: 
101
102 Function  NewthumbSize(currentwidth, currentheight) 
103 dim  tempMultiplier  as   Double  
104 if  currentheight > currentwidth  then  
105 tempMultiplier  =   200   /  currentheight 
106 Else  
107 tempMultiplier  =   200   /  currentwidth 
108 end   if  
109 dim  NewSize  as   New  Size( CInt (currentwidth  *  tempMultiplier),  CInt (currentheight  *  tempMultiplier)) 
110 return  NewSize 
111 End Function
 
112
113
114    以上程序是增加的一个函数NewthumbSize,该函数专门处理改变一会的图片大小,这个图片的长宽和原图片的长宽保持相同比例。其他部分请参考上文程序代码。 
115 三、画图特效 
116
117    如果只是将图片显示在网页上,这样未免显得简单。现在,我们来进一步感受ASP.NET的强大功能。我们将学习图象处理中常用的图象反转、图象切割、图象拉伸等技巧。 
118 先来看看程序效果: 
119
120  
121  仔细看,我们可以找到各种图象处理效果。现在,我们来看看程序代码: 
122
123 <%@ Page Language = " vb "  Debug = " True "  %> 
124 <%@ import  namespace = " system.drawing "  %> 
125 <%@ import  namespace = " system.drawing.imaging "  %> 
126 <%@ import  namespace = " system.drawing.drawing2d "  %> 
127 <% 
128 dim  strFilename  as   string  
129 dim  i  as  System.Drawing.Image 
130 strFilename  =  server.mappath( " ./chris-fsck.jpg "
131
132 =  System.Drawing.Image.FromFile(strFilename) 
133
134 dim  b  as   New  system.drawing.bitmap(i.width, i.height, pixelformat.format24bpprgb) 
135 dim  g  as  graphics  =  graphics.fromimage(b) 
136
137 g.clear(color.blue) 
138
139 ' 旋转图片 
140 i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX) 
141 g.drawimage(i, New  point( 0 , 0 )) 
142 i.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipY) 
143
144 g.RotateTransform( 10
145 g.drawimage(i, New  point( 0 , 0 )) 
146 g.RotateTransform( 10
147 g.drawimage(i, New  point( 20 , 20 )) 
148 g.RotateTransform( 10
149 g.drawimage(i, New  point( 40 , 40 )) 
150 g.RotateTransform( 10
151 g.drawimage(i, New  point( 40 , 40 )) 
152 g.RotateTransform( - 40
153 g.RotateTransform( 90
154 g.drawimage(i, New  rectangle( 100 , - 400 , 100 , 50 ), New  rectangle( 20 , 20 ,i.width - 20 ,i.height - 20 ),GraphicsUnit.Pixel) 
155 g.RotateTransform( - 90
156
157 '  拉伸图片 
158 g.drawimage(i, New  rectangle( 10 , 10 , 50 , 50 ), New  rectangle( 20 , 20 ,i.width - 20 ,i.height - 20 ),GraphicsUnit.Pixel) 
159 g.drawimage(i, New  rectangle( 50 , 10 , 90 , 50 ), New  rectangle( 20 , 20 ,i.width - 20 ,i.height - 20 ),GraphicsUnit.Pixel) 
160 g.drawimage(i, New  rectangle( 110 , 10 , 150 , 50 ), New  rectangle( 20 , 20 ,i.width - 20 ,i.height - 20 ),GraphicsUnit.Pixel) 
161
162
163 ' 切割图片 
164 g.drawimage(i, 50 , 100 , New  rectangle( 180 , 80 , 60 , 110 ),GraphicsUnit.Pixel) 
165 g.drawimage(i, 140 , 100 , New  rectangle( 180 , 80 , 60 , 110 ),GraphicsUnit.Pixel) 
166
167 ' 旋转图片 
168 i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX) 
169 g.drawimage(i, 230 , 100 , New  rectangle( 180 , 110 , 60 , 110 ),GraphicsUnit.Pixel) 
170
171 response.contenttype = " image/jpeg "  
172
173 b.save(response.outputstream, imageformat.jpeg) 
174
175 b.dispose() 
176
177 %> 
178
179
180    在以上的程序中,我们看到实现图象处理的各种技巧,仔细观察,我们可以知道旋转图片其实是用了一个RotateFlip方法;而切割和拉伸图片,完全是通过设置DrawImage的不同参数来实现。 
181
182    四、总结 
183
184    ASP.NET的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要我们在实践中摸索、总结。
185
186 点击数: 141     更新时间: 2006 - 4 - 26
posted on 2006-08-16 17:00 曹贤 阅读(897) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: