473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Avoiding Image Caching

I have been trying to find a way to prevent the images on my site from being
cached on the user's machine. I want to avoid this because the images used
in an Image control often change, yet have the same name. When they are
cached, the cached image is displayed which is not what I want. I thought
that using
Response.Cache.SetCacheability(HttpCacheability.No Cache)

would have achieved this, but it seems to only prevent the .aspx file from
being cached. Is there any way to prevent images from being cached as well?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Aug 13 '05 #1
7 1943
Nathan,

I am not sure if this will do it (I did not try it). In this sample I make
the image in the procedure, however you can do it of course using a read.
Now you can get the image by its page url

\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Response.Cache.SetExpires(DateTime.Now.AddTicks(50 0))
Dim myForeBrush As Brush = Brushes.Black
Dim myFont As New Font("Times New Roman", 8, FontStyle.Regular)
Dim textHeight As Single
Dim bm As New Bitmap(120, 20)
Dim g As Graphics = Graphics.FromImage(bm)
g.Clear(Color.White)
Dim textSize As SizeF = g.MeasureString(now.tostring, myFont)
g.DrawString(Now.ToString, myFont, myForeBrush, _
New RectangleF(0, 0, 120, 20))
Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
bm.Save(ms, Imaging.ImageFormat.Bmp)
arrImage = ms.GetBuffer
Response.BinaryWrite(arrImage)
g.dispose
End Sub
///

I hope this helps,

Cor
Aug 13 '05 #2
I don't think it is caching. Check that the new image on the server has a
later create date than the older, if not the older will be shown.
Test this by having 2 images, aaa.jpg, put the one with the older creation
date on the server and browse to it, now put the newer one on the server,
browse to it and the 2nd one will show, all OK.
Now rename them to bbb.jpg, first put the one with the newer creation date
on the server, browse, now the older creation date, browse, the 1st image
still shows up.

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:#Q**************@TK2MSFTNGP15.phx.gbl...
I have been trying to find a way to prevent the images on my site from being cached on the user's machine. I want to avoid this because the images used
in an Image control often change, yet have the same name. When they are
cached, the cached image is displayed which is not what I want. I thought
that using
Response.Cache.SetCacheability(HttpCacheability.No Cache)

would have achieved this, but it seems to only prevent the .aspx file from
being cached. Is there any way to prevent images from being cached as well? --
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Aug 13 '05 #3
I agree with Chris - but if you want to make sure this doesn't happen, use a
HTTPHandler to draw your own images - then they can't steal your bandwidth
either ;-) http://www.uberasp.net/GetArticle.aspx?id=13
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
Nathan,

I am not sure if this will do it (I did not try it). In this sample I make
the image in the procedure, however you can do it of course using a read.
Now you can get the image by its page url

\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Response.Cache.SetExpires(DateTime.Now.AddTicks(50 0))
Dim myForeBrush As Brush = Brushes.Black
Dim myFont As New Font("Times New Roman", 8, FontStyle.Regular)
Dim textHeight As Single
Dim bm As New Bitmap(120, 20)
Dim g As Graphics = Graphics.FromImage(bm)
g.Clear(Color.White)
Dim textSize As SizeF = g.MeasureString(now.tostring, myFont)
g.DrawString(Now.ToString, myFont, myForeBrush, _
New RectangleF(0, 0, 120, 20))
Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
bm.Save(ms, Imaging.ImageFormat.Bmp)
arrImage = ms.GetBuffer
Response.BinaryWrite(arrImage)
g.dispose
End Sub
///

I hope this helps,

Cor

Aug 13 '05 #4
Well what you can also do is when generating the URL for the image,
append a random string to it like this:

image.ImageUrl = "someimage.gif?randomstring"

and then the browser will get the fresh image as long as the
randomstring is different. For random string you can use TickCount or
anything else you want. Of course there are some drawbacks to this
technique (such as brwoser getting image even though it hasn't changed).

L
Aug 13 '05 #5
Chris Botha wrote:
I don't think it is caching.


Think again. If an image is being cached on the client-side or some
downstream proxy, you can change it on the server-side all day long
without affecting the client.

You *must* specify the appropriate Cache-Control header
("must-revalidate" in theory, thanks to IE "no-cache" in practice) to
force clients and proxies to send conditional GETs and pick up
server-side updates.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Aug 17 '05 #6
You can configure this in IIS MMC by navigating to the folder containing the
images and adjusting the HTTP Headers to either "expire immediately" or
"never expire".

--
Benjamin Strackany
http://www.developmentnow.com
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I have been trying to find a way to prevent the images on my site from being cached on the user's machine. I want to avoid this because the images used
in an Image control often change, yet have the same name. When they are
cached, the cached image is displayed which is not what I want. I thought
that using
Response.Cache.SetCacheability(HttpCacheability.No Cache)

would have achieved this, but it seems to only prevent the .aspx file from
being cached. Is there any way to prevent images from being cached as well? --
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Aug 17 '05 #7
Bill,
I agree with Chris - but if you want to make sure this doesn't happen, use
a HTTPHandler to draw your own images - then they can't steal your
bandwidth either ;-)


I had the idea that that is what I am doing in my sample

:-)

The bitmap is embedded in the url by the and sent as bitmap by the redirect.

It is not a reference to an image url.

However if you disagree that with me, please tell where I make a mistake in
that idea?

Cor
Aug 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fred | last post by:
Hello As mentioned in another post, I'm in the process of extending an existing class. One of the things I'm trying to achieve is an improvement in performance through the use of some image...
2
by: Marwin | last post by:
Hi all, I have a database with 40GB of binary objects stored in image columns in two tables. Our database server is also used for another 15 databases. SQL Server caches the image-column,...
2
by: Mike Sobeiski | last post by:
Hi There! I'm developing a web project that requires to produce charts off the database and output it as image and presented to the user in a HTML document. (NOTE: Image is not directly...
6
by: Suraj Joneja | last post by:
Hi All, I've an image control on my ASP.net page. This displays an image named 'Logo.jpg' in the location '~\Images'. Another application can change this image. It can select any image and...
4
by: Jake | last post by:
Does cookieless session state (with the sessionid embedded into the url) interfere with the browser's retrieval of cached images from one session to the next? Does the sessionid embedded into the...
7
by: Nathan Sokalski | last post by:
I have been trying to find a way to prevent the images on my site from being cached on the user's machine. I want to avoid this because the images used in an Image control often change, yet have...
1
by: Giggle Girl | last post by:
Hello, I am helping to design a web application that uses 167 little icons on various pages in a framed environment. As is, the app loads each icon on a page by page basis, and if an image...
9
by: Mark Denardo | last post by:
This is related to another post I submitted, but I'll be more precise this time. I have a web page that contains an Image webcontrol that loads its image by: Image1.ImageUrl="<username>.jpg",...
4
by: vunet.us | last post by:
Hi, I found a bug in IE6, though it is known already. If I have a CSS background property set to some image, such as background:url(myimg.gif);, and I apply this property to some html element,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.