<% "清空response response.clear "建立一個120*30大小,24bit的bmp圖象; dim imgoutput as new bitmap(120, 30, pixelformat.format24bpprgb) "根據以上bmp建立一個新圖象; dim g as graphics = graphics.fromimage(imgoutput) g.clear(color.green) g.smoothingmode = smoothingmode.antialias g.drawstring("看見了嗎?", new font("黑體",16,fontstyle.bold),new solidbrush(color.white),new pointf(2,4)) g.fillrectangle(new lineargradientbrush(new point(0,0), new point(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不就可以了?當然可以,我們這里只是提供一種選擇和方法來實現這一功能,具體這一功能的使用,我們可能需要在實踐中更多的學習。先來看程序源代碼: <% " 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的不同參數來實現。