Problem creating images on the fly

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
 
H

Herfried K. Wagner [MVP]

Brian Cryer said:
<img
src="rectangle.aspx?width=30&height=20&colour=FF0000&border=C0C0C0">

This won't solve your problem, but don't forget to encode the ampersands:

\\\
<img
src="rectangle.aspx?width=30&amp;height=20&amp;colour=FF0000&amp;border=C0C0C0"/>
///
 
L

Larry Serflaten

Brian Cryer said:
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">

Was there some reason you did not try using HTML objects to build your
colored rectangles? Something like the DIV object seems like it would suit
your needs, without having to build bitmaps....

LFS
 
B

Brian Cryer

Herfried K. Wagner said:
This won't solve your problem, but don't forget to encode the ampersands:

\\\
<img
src="rectangle.aspx?width=30&amp;height=20&amp;colour=FF0000&amp;border=C0C0
C0"/>
///

Herfried,

Ouch (kicks himself) very good point, (but you're right ... it doesn't solve
my problem).

Thanks,

Brian.

www.cryer.co.uk/brian
 
B

Brian Cryer

Larry Serflaten said:
Was there some reason you did not try using HTML objects to build your
colored rectangles? Something like the DIV object seems like it would suit
your needs, without having to build bitmaps....

LFS

Good point. I started out using a table and setting the background colour.
That worked fine until some people printed it and the colours didn't come
out. I realise this is down to their configuration Tools > Internet Options
Advanced > Printing with "Print background colors and images" unchecked,
but telling the users to change their settings isn't an ideal solution.

I assume that if I were to use DIVs then I would hit the same problem?
Please correct me if I'm wrong.

thanks,

Brian.
 
L

Larry Serflaten

Brian Cryer said:
Good point. I started out using a table and setting the background colour.
That worked fine until some people printed it and the colours didn't come
out. I realise this is down to their configuration Tools > Internet Options
but telling the users to change their settings isn't an ideal solution.

I assume that if I were to use DIVs then I would hit the same problem?
Please correct me if I'm wrong.

I don't know for sure, give it try and see....

LFS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top