B
Brian Cryer
I'm developing a website and for one of the pages I need to create a number
of coloured rectangles - its part of a legend and the user has control over
the actual colours used which is why I want to create it dynamically. I've
put together an aspx.vb page which creates a rectangle as a jpeg in the
colour passed via the url. My idea being to pull in each coloured rectangle
using something like:
<img
src="rectangle.aspx?width=30&height=20&colour=FF0000&border=C0C0C0">
This SEEMS to work, BUT the first time I view the page all but one of my
images fails to load. (Explorer displays the symbol for a missing image.) If
I refresh the page (or visit it again a few minutes later) then its fine.
Any ideas what I could be doing wrong?
If its relevant, my code for generating the image is:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here.
' Extract the definition of the image we require.
Dim width As Integer =
Val(HttpContext.Current.Request.Params("width"))
If width = 0 Then
width = 20
End If
Dim height As Integer =
Val(HttpContext.Current.Request.Params("height"))
If height = 0 Then
height = 20
End If
Dim colour As String = HttpContext.Current.Request.Params("colour")
Dim border As String = HttpContext.Current.Request.Params("border")
' Draw rectangle.
Dim bitmap As New Bitmap(width, height)
Dim graph As Graphics = Graphics.FromImage(bitmap)
Dim fillBrush As New SolidBrush(HTMLtoRGB(colour))
If border = "" Then
graph.FillRectangle(fillBrush, 0, 0, width, height)
Else
Dim outlinePen As New Pen(HTMLtoRGB(border), 1)
graph.DrawRectangle(outlinePen, 0, 0, width, height)
graph.FillRectangle(fillBrush, 1, 1, width - 2, height - 2)
outlinePen.Dispose()
End If
fillBrush.Dispose()
' Send this to the output.
Dim memStream As New MemoryStream
Response.Clear()
Response.ContentType = "image/jpeg"
bitmap.Save(memStream, ImageFormat.Jpeg)
memStream.WriteTo(Response.OutputStream)
' Tidy up.
bitmap.Dispose()
End Sub
thanks in advance,
Brian Cryer
www.cryer.co.uk/brian
of coloured rectangles - its part of a legend and the user has control over
the actual colours used which is why I want to create it dynamically. I've
put together an aspx.vb page which creates a rectangle as a jpeg in the
colour passed via the url. My idea being to pull in each coloured rectangle
using something like:
<img
src="rectangle.aspx?width=30&height=20&colour=FF0000&border=C0C0C0">
This SEEMS to work, BUT the first time I view the page all but one of my
images fails to load. (Explorer displays the symbol for a missing image.) If
I refresh the page (or visit it again a few minutes later) then its fine.
Any ideas what I could be doing wrong?
If its relevant, my code for generating the image is:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here.
' Extract the definition of the image we require.
Dim width As Integer =
Val(HttpContext.Current.Request.Params("width"))
If width = 0 Then
width = 20
End If
Dim height As Integer =
Val(HttpContext.Current.Request.Params("height"))
If height = 0 Then
height = 20
End If
Dim colour As String = HttpContext.Current.Request.Params("colour")
Dim border As String = HttpContext.Current.Request.Params("border")
' Draw rectangle.
Dim bitmap As New Bitmap(width, height)
Dim graph As Graphics = Graphics.FromImage(bitmap)
Dim fillBrush As New SolidBrush(HTMLtoRGB(colour))
If border = "" Then
graph.FillRectangle(fillBrush, 0, 0, width, height)
Else
Dim outlinePen As New Pen(HTMLtoRGB(border), 1)
graph.DrawRectangle(outlinePen, 0, 0, width, height)
graph.FillRectangle(fillBrush, 1, 1, width - 2, height - 2)
outlinePen.Dispose()
End If
fillBrush.Dispose()
' Send this to the output.
Dim memStream As New MemoryStream
Response.Clear()
Response.ContentType = "image/jpeg"
bitmap.Save(memStream, ImageFormat.Jpeg)
memStream.WriteTo(Response.OutputStream)
' Tidy up.
bitmap.Dispose()
End Sub
thanks in advance,
Brian Cryer
www.cryer.co.uk/brian