By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

Problem creating images on the fly

P: n/a
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=FF00 00&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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Brian Cryer" <br****@127.0.0.1.activesol.co.uk> schrieb:
<img
src="rectangle.aspx?width=30&height=20&colour=FF00 00&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;col our=FF0000&amp;border=C0C0C0"/>
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

P: n/a

"Brian Cryer" <br****@127.0.0.1.activesol.co.uk> wrote
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=FF00 00&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

Nov 21 '05 #3

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OZ**************@TK2MSFTNGP09.phx.gbl...
"Brian Cryer" <br****@127.0.0.1.activesol.co.uk> schrieb:
<img
src="rectangle.aspx?width=30&height=20&colour=FF00 00&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;col our=FF0000&amp;border=C0C0
C0"/> ///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


Herfried,

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

Thanks,

Brian.

www.cryer.co.uk/brian
Nov 21 '05 #4

P: n/a
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...

"Brian Cryer" <br****@127.0.0.1.activesol.co.uk> wrote
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=FF00 00&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
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.

Nov 21 '05 #5

P: n/a

"Brian Cryer" <br****@127.0.0.1.activesol.co.uk> wrote
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....


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.


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

LFS
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.