ASP.NET中圖象處理過程詳__第1頁
ASP.NET中圖象處理過程詳__第2頁
ASP.NET中圖象處理過程詳__第3頁
ASP.NET中圖象處理過程詳__第4頁
ASP.NET中圖象處理過程詳__第5頁
免費預(yù)覽已結(jié)束,剩余6頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、ASP.NET中圖象處理過程詳_ 在用法ASP的時候,我們時常要借助第三方控件來實現(xiàn)一些圖象功能。而現(xiàn)在,ASP.NET的推出,我們已經(jīng)沒有必要再用法第三方控件來實現(xiàn),由于ASP.NET已經(jīng)具有強大的功能來實現(xiàn)一些圖象處理?,F(xiàn)在,我們就來看看怎樣用法ASP.NET的這一強大功能。 一、System.Drawing的用法 以下的舉例將演示在內(nèi)存中生成一張圖片,然后,將這張圖片通過網(wǎng)頁顯示出來。需要了解的是,我們這里輸出的不是HTML效果,而是實實在在的圖片(圖象),我們可以用法“另存為”將輸出圖象保存起來。 我們先來看看效果: 我們看到,這張圖片是一個漸變背景上有“觀察了嗎”幾個字,當(dāng)然,這個效

2、果在PhotoShop等圖象處理軟件里面很簡單實現(xiàn),但是,一些與數(shù)據(jù)庫結(jié)合 的應(yīng)用我們不行能將全部圖片都事先設(shè)計出來,這時候,利用ASP.NET來實現(xiàn)這些功能就顯得很重要了。我們來看源代碼: %pagelanguage=vbcontenttype=image/jpeg% %importnamespace=system.drawing% %importnamespace=system.drawing.imaging% %importnamespace=system.drawing.drawing2d% % 清空Response response.clear 建立一個120*30大小,24bit的

3、BMP圖象; dimimgOutputasNewbitmap(120,30,pixelformat.format24bpprgb) 依據(jù)以上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(Newli

4、nearGradientBrush(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 % 在以上代碼中,我們看到和數(shù)據(jù)庫程序不同,這里特地引入了圖象處理的名字空間system.drawing等。程序首先清空了Response,確保沒 有輸出;然后,程

5、序建立了一個120乘30大的BMP圖象,再在這個基礎(chǔ)上建立一個新圖象,建立圖象以后,我們首先“畫”出了字符串“觀察了嗎”,該字符 串為16大粗黑體,顏色為白色,位置為(2,4);最終,我們實現(xiàn)漸變效果。 以上舉例很簡潔,但是假如和數(shù)據(jù)庫結(jié)合,我們可以實現(xiàn)許多用法ASP可能不敢想的效果。 二、讀取和轉(zhuǎn)變圖象文件大小 讀取圖片?挺直用法HTML不就可以了?當(dāng)然可以,我們這里只是供應(yīng)一種選擇和方法來實現(xiàn)這一功能,具體這一功能的用法,我們可能需要在實踐中更多的學(xué)習(xí)。先來看程序源代碼: %importallrelevantnamespaces% %importnamespace=System% %imp

6、ortnamespace=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

7、(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.format

8、24bpprgb) 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 % re

9、sponse.clear ifrequest(src)=orrequest(height)=orrequest(width)=then callsendError() else iffile.exists(server.mappath(request(src)then callsendFile() else callsendError() endif endif response.end % 在以上的程序中,我們看到兩個函數(shù),一個是SendFile,這一函數(shù)主要功能為顯示服務(wù)器上的圖片,該圖片的大小通過Width和Height設(shè)置, 同時,程序會自動檢測圖片類型;另外一個是SendError,

10、這一函數(shù)的主要功能為服務(wù)器上的圖片文件不存在時,顯示錯誤信息,這里很好玩,錯誤信息也 是通過圖片給出的(如圖): 以上的程序顯示圖片并且轉(zhuǎn)變圖片大小,現(xiàn)在,我們將這個程序進一步,顯示圖片并且保持圖片的長寬比例,這樣,和實際應(yīng)用可能比較接近,格外是需要制作電子相冊或者是圖片網(wǎng)站的時候比較有用。我們先來看主要函數(shù): FunctionNewthumbSize(currentwidth,currentheight) dimtempMultiplierasDouble ifcurrentheightcurrentwidththen tempMultiplier=200/currentheight Els

11、e tempMultiplier=200/currentwidth endif dimNewSizeasNewSize(CInt(currentwidth*tempMultiplier),CInt(currentheight*tempMultiplier) returnNewSize EndFunction 以上程序是增加的一個函數(shù)NewthumbSize,該函數(shù)特地處理轉(zhuǎn)變一會的圖片大小,這個圖片的長寬和原圖片的長寬保持相同比例。其他部分請參考上文程序代碼。 三、畫圖特效 假如只是將圖片顯示在網(wǎng)頁上,這樣未免顯得簡潔?,F(xiàn)在,我們來進一步感受ASP.NET的強大功能。我們將學(xué)習(xí)圖象處理中常用的

12、圖象反轉(zhuǎn)、圖象切割、圖象拉伸等技巧。 先來看看程序效果: 認(rèn)真看,我們可以找到各種圖象處理效果?,F(xiàn)在,我們來看看程序代碼: %PageLanguage=vbDebug=True% %importnamespace=system.drawing% %importnamespace=system.drawing.imaging% %importnamespace=system.drawing.drawing2d% % dimstrFilenameasstring dimiasSystem.Drawing.Image strFilename=server.mappath(./chris-fsck.j

13、pg) i=System.Drawing.Image.FromFile(strFilename) dimbasNewsystem.drawing.bitmap(i.width,i.height,pixelformat.format24bpprgb) dimgasgraphics=graphics.fromimage(b) g.clear(color.blue) 旋轉(zhuǎn)圖片 i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX) g.drawimage(i,Newpoint(0,0) i.RotateFlip(System.Drawing

14、.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,New

15、rectangle(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-

16、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) 旋轉(zhuǎn)圖片 i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX) g.drawimage(i,230,100,Newrectangle(180,110,60,110),GraphicsUnit.Pixel) response.cont

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論