Oh well, was able to do it after all
Create ImageHttpModule class file and insert the following class
declaration:
Public Class ImageHttpModule
Implements IHttpModule
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
' MustOverride Method.
End Sub
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler context.BeginRequest, AddressOf Me.OnBeginRequest
End Sub
Public Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim context As HttpApplication = DirectCast(sender, HttpApplication)
If context.Request.Path.ToLower().IndexOf("image_number.aspx") < 0
Return
End If
' Should really generate the random number and store in a database,
' then do the fetch on that number in the Try...Catch below,
' if the number is not in the database, then generate one and
' store in the database.
Dim num As Integer = New Random().Next(10000, 99999)
' Build the image.
Dim memStream As IO.MemoryStream
Try
memStream = RenderImage(num)
memStream.WriteTo(context.Context.Response.OutputStream)
memStream.Close()
memStream = Nothing ' Prevents the Close() call in Finally.
context.Context.ClearError()
context.Response.ContentType = "image/gif"
context.Response.StatusCode = 200
context.Response.End()
Catch
context.Response.StatusCode = 500
context.Response.End()
Finally
If Not memStream Is Nothing
memStream.Close()
End If
End Try
End Sub
Private Function RenderImage(ByVal Number As Integer) As IO.MemoryStream
Dim canvas As Graphics = Graphics.FromImage(New Bitmap(1, 1))
Dim size As SizeF
Dim numberFont As Font = New Font(New FontFamily("Verdana"), 12)
size = canvas.MeasureString( _
CStr(Number), _
numberFont _
)
size.Width += 10
size.Height += 10
Dim image As New Bitmap(size.Width, size.Height,
Imaging.PixelFormat.Format24bppRgb)
canvas = Graphics.FromImage(image)
Try
canvas.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
canvas.TextRenderingHint =
Drawing.Text.TextRenderingHint.SingleBitPerPixel
' Draw background.
Dim backRect As Rectangle = _
New Rectangle(0, 0, image.Width, image.Height)
canvas.Clear(Color.Black)
Dim brush As Brush = New SolidBrush(Color.White)
canvas.DrawString(CStr(Number), numberFont, brush, 5, 5)
Dim memStream As IO.MemoryStream = New IO.MemoryStream()
image.Save(memStream, Imaging.ImageFormat.Gif)
Return memStream
Finally
image.Dispose()
canvas.Dispose()
End Try
End Function
End Class
In the web form code-behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack
imgNumber.ImageUrl = "image_number.aspx"
End If
End Sub
In web.config before </system.web>:
<httpModules>
<add name="PictureImage" type="PictureSample.ImageHttpModule,
PictureSample" />
</httpModules>
Think that's it, hope it works for you
Mythran