asp.net|特效 一般的圖形處理包括圖形的放大,圖形的縮小,圖形的旋轉和展開效果等。這一節中,通過執行個體我們來學習利用GDI+來處理這些操作。
建立一個Web表單,命名為GDI_Sample5.aspx, GDI_Sample5.aspx.vb中的邏輯代碼如下:
'------code begin-----
'--省略命名空間引用
Public Class GDI_Sample5
Inherits System.Web.UI.Page
#Region " Web Form設計器產生的程式碼 "
'此處省略表單設計器產生的程式碼
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此處放置初始化頁的使用者代碼
Dim i As Int16
Dim b As Bitmap
b = New Bitmap(300, 1000)
'聲明Graphics對象
Dim g As Graphics
'指定畫布
g = Graphics.FromImage(b)
'設定背景色為黑色
g.Clear(Color.Black)
Dim img As Drawing.Image
img = Drawing.Image.FromFile(Server.MapPath(".") & "/test.jpg")
'原圖
g.DrawString("-=原圖=-", New Font("宋體", 12, FontStyle.Bold), Brushes.White, New PointF(80, 2))
g.DrawImage(img, 50, 20, img.Width, img.Height)
'放大
g.DrawLine(New Pen(Color.White, 1), 10, 165, 280, 165)
g.DrawString("-=放大2倍圖=-", New Font("宋體", 12, FontStyle.Bold), Brushes.White, New PointF(80, 168))
g.DrawImage(img, 20, 190, CInt(img.Width * 1.4), CInt(img.Height * 1.4))
'縮小
g.DrawLine(New Pen(Color.White, 1), 10, 410, 280, 410)
g.DrawString("-=縮小1/2倍圖=-", New Font("宋體", 12, FontStyle.Bold), Brushes.White, New PointF(80, 415))
g.DrawImage(img, 80, 435, CInt(img.Width / 1.4), CInt(img.Height / 1.4))
'展開
g.DrawLine(New Pen(Color.White, 1), 10, 775, 280, 775)
g.DrawString("-=展開效果=-", New Font("宋體", 12, FontStyle.Bold), Brushes.White, New PointF(80, 780))
g.DrawImage(img, 10, 795, img.Width + 100, img.Height - 50)
'旋轉
g.DrawLine(New Pen(Color.White, 1), 10, 560, 280, 560)
g.DrawString("-=旋轉90度=-", New Font("宋體", 12, FontStyle.Bold), Brushes.White, New PointF(80, 565))
img.RotateFlip(RotateFlipType.Rotate90FlipNone)
g.DrawImage(img, 80, 590, img.Width, img.Height)
'匯出到客戶瀏覽器中,由於asp.net繪製的圖片使用了安全色,所以在瀏覽器查看時候會造成失色
b.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Gif)
'另儲存為該Web應用程式的實體路徑目錄下的test1.jpg檔案
b.Save(Server.MapPath(".") & "/test1.jpg")
End Sub
End Class
'-----code end --------
儲存編譯後,結果下圖11.5所示。
圖11.5 GDI_Sample5.aspx運行結果
由於輸出到用戶端瀏覽器中的映像是採用安全色顯示,所以在通過瀏覽器瀏覽時映像會失色,但是在硬碟中儲存的映像是原色,不會有失色現象的。
11.6 本章小結
本章簡要的介紹了利用GDI+技術繪製圖形的方法。GDI+是全新的圖形案頭引擎,是一個應用編程介面,GDI+是對GDI的高層次的一種封裝類.Graphics對象是繪圖操作中唯一保留的對象.用.NET產生圖形的功能封裝在System.Drawing名稱空間中,它支援多種圖形檔案格式,包括:JPEG、GIF、PNG、TIFF、BMP、PhotoCD、FlashPIX、WMF、EMF和EXIF。利用BitMap對象和Graphics對象就可以輕鬆的實現一些簡單的圖形處理需求。當然,GDI+技術的繪製圖形功能絕非一個章節能詳細列盡,本章只是簡要的介紹,不是本書重點,有興趣的讀者可以參閱有關專門書籍。下一章我們將學習ASP.NET的安全控制知識。