Solved

Problem with Image not refreshing

Posted on 2008-10-03
6
402 Views
Last Modified: 2012-08-13
Hi all

I am using image verification (CAPTCHA) on my contact form and for some reason the image is not being updated when the page refreshes. The image tag...

<asp:Image ID="verimg" runat="server" Width="100" Height="40" />

..has its imageurl property set programmatically when the new image is generated and saved (see code below) and I was expecting the image to be refreshed whenever the following two lines were executed by the code..

Dim code As String = GenerateVCodeImage()
Session("VCode") = code

..but this isn't happening.

I have also included the following three lines of code in my page_load routine to stop cacheing, but that hasn't helped..

Response.Expires = 0
Response.Cache.SetNoStore()
Response.AppendHeader("Pragma", "no-cache")

Can anyone suggest why my image won't refresh, or provide a better solution for displaying my generated bitmap on the contact form.

Thanks in advance

Jerute
Private Function GenerateVCodeImage() As String
 

	Dim bmfile As String = Server.MapPath("~") & "\capimage.jpg"

	Dim txfile As String = Server.MapPath("~") & "\captext.txt"

	Dim oBitmap As Drawing.Bitmap = New Drawing.Bitmap(90, 35)

	Dim oGraphic As Drawing.Graphics = Drawing.Graphics.FromImage(oBitmap)

	Dim foreColor As System.Drawing.Color

	Dim backColor As System.Drawing.Color
 

	Dim sText As String = generateVCode(5)

	Dim sFont As String = "Comic Sans MS"
 

	foreColor = System.Drawing.Color.FromArgb(220, 220, 220)

	backColor = System.Drawing.Color.FromArgb(190, 190, 190)
 

	Dim oBrush As New Drawing.Drawing2D.HatchBrush(CType(generateHatchStyle(), Drawing.Drawing2D.HatchStyle), foreColor, backColor)

	Dim oBrushWrite As New Drawing.SolidBrush(Drawing.Color.Red)
 

	oGraphic.FillRectangle(oBrush, 0, 0, 100, 50)

	oGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
 

	Dim oFont As New Drawing.Font(sFont, 14)

	Dim oPoint As New Drawing.PointF(5.0F, 4.0F)
 

	oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint)
 

	Response.ContentType = "image/jpeg"

	oBitmap.Save(bmfile, Drawing.Imaging.ImageFormat.Jpeg)

	oBitmap.Dispose()
 

	verimg.ImageUrl = "capimage.jpg"

	File.WriteAllText(txfile, sText)

	Return sText
 

End Function
 
 

Private Function generateVCode(ByVal CodeLength As Integer) As String

	Dim VCode As String = String.Empty

	Dim randObj As New Random()

	Dim c As Integer = 63

	For i As Byte = 1 To CodeLength

		c = randObj.Next(35)

		If c >= 10 Then

			c += 7

		End If

		c += 48

		VCode += Chr(c)

	Next

	Return VCode

End Function
 

Private Function generateHatchStyle() As HatchStyle

	Dim slist As New ArrayList

	For Each style As HatchStyle In System.Enum.GetValues(GetType(HatchStyle))

		slist.Add(style)

	Next
 

	Dim randObj As New Random()

	Dim index As Integer = randObj.Next(slist.Count - 1)
 

	Return CType(slist(index), HatchStyle)

End Function

Open in new window

0
Comment
Question by:jerute
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:Genome_A_W
ID: 22631867
back when i used to make websites i used to get a problem with the images not updating. Id check everything and by all means they should have refreshed. I would give up and then the next day (or sometimes a few hours later) they would have changed. Something to do with the cache server side maybe?
0
 
LVL 10

Accepted Solution

by:
ericwong27 earned 500 total points
ID: 22633292
Try to generate a unique ID string and append it to the image Like:

verimg.ImageUrl = "capimage.jpg?uid=" + System.Guid.NewGuid().ToString()

0
 

Author Comment

by:jerute
ID: 22633393
Thanks for your reply Eric but I'll have to stand up to read this as it just went over my head. I've never dealt with unique IDs. Can you explain the how's and why's?

Thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:ericwong27
ID: 22634168
This will trick the browser into reloading the image from the server since the image name is difference. if your image name is not change, the browser probably get it from cache.
0
 

Author Comment

by:jerute
ID: 22635620
You mistunderstand me Eric. How do I implement this GUID?

How do I assign one, and how do I use it? Can you give me an example as I have never come across the use of these before.

Thanks again.
0
 

Author Comment

by:jerute
ID: 22639350
A-HA!! I get it. Yes, that seems to do the trick. Thank you EW.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
No row return after calling the fillschema method 4 40
Spacing between controls 4 30
Close word object 13 38
Populating an array of locale_time_info 8 37
Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn the basics of inputting and editing your text components in Prezi. We will cover how to set styles, position, and group your text components. In your Prezi editor, click anywhere on the canvas to add text: A flashing cursor informs you that yo…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now