二、读取和改变图象文件大小 
  
  读取图片?直接使用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,该函数专门处理改变一会的图片大小,这个图片的长宽和原图片的长宽保持相同比例。其他部分请参考上文程序代码。

posted on 2006-11-20 13:54  JerryZhao  阅读(145)  评论(0)    收藏  举报