二、讀取和改變圖象文件大小 讀取圖片?直接使用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的強大功能。我們將學習圖象處理中常用的圖象反轉、圖象切割、圖象拉伸等技巧。 先來看看程序效果:
仔細看,我們可以找到各種圖象處理效果,F在,我們來看看程序代碼: <%@ 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的圖象處理可以實現的功能很多,我們在這里其實只是簡單的介紹,更多功能的應用,需要我們在實踐中摸索、總結。
|