网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 ASP.NET中图象处理过程详
内容
    在使用ASP的时候,我们时常要借助第三方控件来实现一些图象功能。而现在,ASP.NET的推出,我们已经没有必要再使用第三方控件来实现,因为ASP.NET已经具有强大的功能来实现一些图象处理。现在,我们就来看看怎样使用ASP.NET的这一强大功能。
    一、System.Drawing的使用
    以下的举例将演示在内存中生成一张图片,然后,将这张图片通过网页显示出来。需要了解的是,我们这里输出的不是HTML效果,而是实实在在的图片(图象),我们可以使用“另存为…”将输出图象保存起来。
    我们先来看看效果:
    我们看到,这张图片是一个渐变背景上有“看见了吗”几个字,当然,这个效果在PhotoShop等图象处理软件里面很容易实现,但是,一些与数据库结合 的应用我们不可能将所有图片都事先设计出来,这时候,利用ASP.NET来实现这些功能就显得很重要了。我们来看源代码:
    <%@pagelanguage="vb"contenttype="image/jpeg"%>
    <%@importnamespace="system.drawing"%>
    <%@importnamespace="system.drawing.imaging"%>
    <%@importnamespace="system.drawing.drawing2d"%>
    <%
    '清空Response
    response.clear
    '建立一个120*30大小,24bit的BMP图象;
    dimimgOutputasNewbitmap(120,30,pixelformat.format24bpprgb)
    '根据以上BMP建立一个新图象;
    dimgasgraphics=graphics.fromimage(imgOutput)
    g.clear(color.Green)
    g.smoothingMode=smoothingMode.antiAlias
    g.drawString("看见了吗?",Newfont("黑体",16,fontstyle.bold),newSolidBrush(Color.White),NewpointF(2,4))
    g.FillRectangle(NewlinearGradientBrush(Newpoint(0,0),Newpoint(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不就可以了?当然可以,我们这里只是提供一种选择和方法来实现这一功能,具体这一功能的使用,我们可能需要在实践中更多的学习。先来看程序源代码:
    <%'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的图象处理可以实现的功能很多,我们在这里其实只是简单的介绍,更多功能的应用,需要
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/17 16:16:13