2006-11-20
Asp.Net2.0 實作http handler 加入文字在圖片
1.dll code
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Public Class BMPHandler
Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Dim s As String = "text drawing here!"
Dim bmpFile As String = context.Request.PhysicalPath
Dim bmp As Image = Bitmap.FromFile(bmpFile)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim f As New Font("Courier New", 10)
Dim b As New SolidBrush(Color.White)
g.DrawString(s, f, b, 0, 0)
Dim ms As MemoryStream = New MemoryStream
bmp.Save(ms, ImageFormat.Bmp)
context.Response.Clear()
context.Response.ContentType = "IMAGE/BMP"
context.Response.BinaryWrite(ms.ToArray())
context.Response.End()
End Sub
End Class
2.註冊到web.config
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Public Class BMPHandler
Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return True
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Dim s As String = "text drawing here!"
Dim bmpFile As String = context.Request.PhysicalPath
Dim bmp As Image = Bitmap.FromFile(bmpFile)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim f As New Font("Courier New", 10)
Dim b As New SolidBrush(Color.White)
g.DrawString(s, f, b, 0, 0)
Dim ms As MemoryStream = New MemoryStream
bmp.Save(ms, ImageFormat.Bmp)
context.Response.Clear()
context.Response.ContentType = "IMAGE/BMP"
context.Response.BinaryWrite(ms.ToArray())
context.Response.End()
End Sub
End Class
2.註冊到web.config