当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET图象处理详解(2)

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET图象处理详解(2)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 62 ::
收藏到网摘: n/a

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

读取图片?直接使用HTML不就可以了?当然可以,我们这里只是提供一种选择和方法来实现这一功能,具体这一功能的使用,我们可能需要在实践中更多的学习。先来看程序源代码:

<%'importallrelevantnamespaces%>
<%@importnamespace="System"%>
<%@importnamespace="System.Drawing"%>
<%@importnamespace="System.Drawing.Imaging"%>
<%@importnamespace="System.IO"%>

<scriptrunat="server">
SubsendFile()
dimgasSystem.Drawing.Image=System.Drawing.Image.FromFile(server.mappath(request("src")))
dimthisFormat=g.rawformat
dimimgOutputasNewBitmap(g,cint(request("width")),cint(request("height")))
ifthisformat.equals(system.drawing.imaging.imageformat.Gif)then
response.contenttype="image/gif"
else
response.contenttype="image/jpeg"
endif
imgOutput.save(response.outputstream,thisformat)
g.dispose()
imgOutput.dispose()
endsub

SubsendError()
dimimgOutputasNewbitmap(120,120,pixelformat.format24bpprgb)
dimgasgraphics=graphics.fromimage(imgOutput)
g.clear(color.yellow)
g.drawString("错误!",Newfont("黑体",14,fontstyle.bold),systembrushes.windowtext,NewpointF(2,2))
response.contenttype="image/gif"
imgOutput.save(response.outputstream,imageformat.gif)
g.dispose()
imgOutput.dispose()
endsub
</script>

<%
response.clear
ifrequest("src")=""orrequest("height")=""orrequest("width")=""then
callsendError()
else
iffile.exists(server.mappath(request("src")))then
callsendFile()
else
callsendError()
endif
endif
response.end
%>

在以上的程序中,我们看到两个函数,一个是SendFile,这一函数主要功能为显示服务器上的图片,该图片的大小通过Width和Height设置,同时,程序会自动检测图片类型;另外一个是SendError,这一函数的主要功能为服务器上的图片文件不存在时,显示错误信息,这里很有趣,错误信息也是通过图片给出的(如图):

以上的程序显示图片并且改变图片大小,现在,我们将这个程序进一步,显示图片并且保持图片的长宽比例,这样,和实际应用可能比较接近,特别是需要制作电子相册或者是图片网站的时候比较实用。我们先来看主要函数:

FunctionNewthumbSize(currentwidth,currentheight)
dimtempMultiplierasDouble
ifcurrentheight>currentwidththen
tempMultiplier=200/currentheight
Else
tempMultiplier=200/currentwidth
endif
dimNewSizeasNewSize(CInt(currentwidth*tempMultiplier),CInt(currentheight*tempMultiplier))
returnNewSize
EndFunction


以上程序是增加的一个函数NewthumbSize,该函数专门处理改变一会的图片大小,这个图片的长宽和原图片的长宽保持相同比例。其他部分请参考上文程序代码。

三、画图特效

如果只是将图片显示在网页上,这样未免显得简单。现在,我们来进一步感受ASP.NET的强大功能。我们将学习图象处理中常用的图象反转、图象切割、图象拉伸等技巧。
先来看看程序效果:

仔细看,我们可以找到各种图象处理效果。现在,我们来看看程序代码:

<%@PageLanguage="vb"Debug="True"%>
<%@importnamespace="system.drawing"%>
<%@importnamespace="system.drawing.imaging"%>
<%@importnamespace="system.drawing.drawing2d"%>
<%
dimstrFilenameasstring
dimiasSystem.Drawing.Image
strFilename=server.mappath("./chris-fsck.jpg")

i=System.Drawing.Image.FromFile(strFilename)

dimbasNewsystem.drawing.bitmap(i.width,i.height,pixelformat.format24bpprgb)
dimgasgraphics=graphics.fromimage(b)

g.clear(color.blue)

'旋转图片
i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX)
g.drawimage(i,Newpoint(0,0))
i.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipY)

g.RotateTransform(10)
g.drawimage(i,Newpoint(0,0))
g.RotateTransform(10)
g.drawimage(i,Newpoint(20,20))
g.RotateTransform(10)
g.drawimage(i,Newpoint(40,40))
g.RotateTransform(10)
g.drawimage(i,Newpoint(40,40))
g.RotateTransform(-40)
g.RotateTransform(90)
g.drawimage(i,Newrectangle(100,-400,100,50),Newrectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
g.RotateTransform(-90)

'拉伸图片
g.drawimage(i,Newrectangle(10,10,50,50),Newrectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
g.drawimage(i,Newrectangle(50,10,90,50),Newrectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
g.drawimage(i,Newrectangle(110,10,150,50),Newrectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)


'切割图片
g.drawimage(i,50,100,Newrectangle(180,80,60,110),GraphicsUnit.Pixel)
g.drawimage(i,140,100,Newrectangle(180,80,60,110),GraphicsUnit.Pixel)

'旋转图片
i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX)
g.drawimage(i,230,100,Newrectangle(180,110,60,110),GraphicsUnit.Pixel)

response.contenttype="image/jpeg"

b.save(response.outputstream,imageformat.jpeg)

b.dispose()

%>


在以上的程序中,我们看到实现图象处理的各种技巧,仔细观察,我们可以知道旋转图片其实是用了一个RotateFlip方法;而切割和拉伸图片,完全是通过设置DrawImage的不同参数来实现。

四、总结

ASP.NET的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要我们在实践中摸索、总结。