asp.net图象处理详解

来源:转载整理  作者:网络
摘要:在使用asp的时候,我们时常要借助第三方控件来实现一些图象功能。而现在,asp.net的推出,我们已经没有必要再使用第三方控件来实现,因为asp.net已经具有强大的功能来实现一些图象处理。现在,我们就来看看怎样使用asp.net的这一强大功能。…

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

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

【相关文章】好搜一下
浅谈Visual Studio中如何选择本地项目与WEB项目

浅谈Visual Studio中如何选择本地

一般在软件开发中可以选择本地项目模型或者WEB项目模型。在Visual Stud…