最近做網(wǎng)站時,要求上傳能加上水印,就研究了一下相關(guān)的功能。推薦一下程序人生的網(wǎng)站,大家也可以寫一些開發(fā)感悟在上面。在使用ASP的時候,我們時常要借助第三方控件來實(shí)現(xiàn)一些圖象功能。而現(xiàn)在,ASP.NET的推出,我們已經(jīng)沒有必要再使用第三方控件來實(shí)現(xiàn),因?yàn)锳SP.NET 已經(jīng)具有強(qiáng)大的功能來實(shí)現(xiàn)一些圖象處理。現(xiàn)在,我們就來看看怎樣使用ASP.NET的這一強(qiáng)大功能。一、System.Drawing的使用以下的舉例將演示在內(nèi)存中生成一張圖片,然后,將這張圖片通過網(wǎng)頁顯示出來。需要了解的是,我們這里輸出的不是HTML效果,而是實(shí)實(shí)在在的圖片(圖象),我們可以使用“另存為…”將輸出圖象保存起來。我們先來看看效果:
我們看到,這張圖片是一個漸變背景上有“看見了嗎”幾個字,當(dāng)然,這個效果在photoshop等圖象處理軟件里面很容易實(shí)現(xiàn),但是,一些與數(shù)據(jù)庫結(jié)合的應(yīng)用我們不可能將所有圖片都事先設(shè)計(jì)出來,這時候,利用ASP.NET來實(shí)現(xiàn)這些功能就顯得很重要了。我們來看源代碼:<%@ page language="vb" contenttype="image/jpeg" %><%@ import namespace="system.drawing" %><%@ import namespace="system.drawing.imaging" %><%@ import namespace="system.drawing.drawing2d" %><%'清空Responseresponse.clear'建立一個120*30大小,24bit的BMP圖象;dim imgOutput as New bitmap(120, 30, pixelformat.format24bp以上的程序顯示圖片并且改變圖片大小,現(xiàn)在,我們將這個程序進(jìn)一步,顯示圖片并且保持圖片的長寬比例,這樣,和實(shí)際應(yīng)用可能比較接近,特別是需要制作電子相冊或者是圖片網(wǎng)站的時候比較實(shí)用。我們先來看主要函數(shù):Function NewthumbSize(currentwidth, currentheight)dim tempMultiplier as Doubleif currentheight > currentwidth thentempMultiplier = 200 / currentheightElsetempMultiplier = 200 / currentwidthend ifdim NewSize as New Size(CInt(currentwidth * tempMultiplier), CInt(currentheight * tempMultiplier))return NewSizeEnd Function以上程序是增加的一個函數(shù)NewthumbSize,該函數(shù)專門處理改變一會的圖片大小,這個圖片的長寬和原圖片的長寬保持相同比例。其他部分請參考上文程序代碼。三、畫圖特效如果只是將圖片顯示在網(wǎng)頁上,這樣未免顯得簡單。現(xiàn)在,我們來進(jìn)一步感受ASP.NET的強(qiáng)大功能。我們將學(xué)習(xí)圖象處理中常用的圖象反轉(zhuǎn)、圖象切割、圖象拉伸等技巧。先來看看程序效果:
仔細(xì)看,我們可以找到各種圖象處理效果。現(xiàn)在,我們來看看程序代碼:<%@ Page Language="vb" Debug="True" %><%@ import namespace="system.drawing" %><%@ import namespace="system.drawing.imaging" %><%@ import namespace="system.drawing.drawing2d" %><%dim strFilename as stringdim i as System.Drawing.ImagestrFilename = 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)'旋轉(zhuǎn)圖片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)'旋轉(zhuǎn)圖片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()%>在以上的程序中,我們看到實(shí)現(xiàn)圖象處理的各種技巧,仔細(xì)觀察,我們可以知道旋轉(zhuǎn)圖片其實(shí)是用了一個RotateFlip方法;而切割和拉伸圖片,完全是通過設(shè)置DrawImage的不同參數(shù)來實(shí)現(xiàn)。四、總結(jié)ASP.NET的圖象處理可以實(shí)現(xiàn)的功能很多,我們在這里其實(shí)只是簡單的介紹,更多功能的應(yīng)用,需要我們在實(shí)踐中摸索、總結(jié)。
新聞熱點(diǎn)
疑難解答
圖片精選