文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页精文荟萃软件资讯 → ASP.NET图象处理详解2

ASP.NET图象处理详解2

时间:2004/10/8 13:15:00来源:本站整理作者:蓝点我要评论(0)

二、读取和改变图象文件大小


   读取图片?直接使用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的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要我们在实践中摸索、总结。


相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3

最新文章 微信3.6.0测试版更新了微信支付漏洞会造成哪 360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗

人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本