473,503 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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/
Nov 19 '05 #1
7 2013
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
Nov 19 '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/

Nov 19 '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

Nov 19 '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
Nov 19 '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
Nov 19 '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/

Nov 19 '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
Nov 19 '05 #8

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

Similar topics

7
1954
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
2258
by: Kamyar Souri | last post by:
How can I disable Image caching? I use ASP.NET Image web control that it's ImageUrl is constant but the image is being changed by the code. because of cachin I always see the same image in my...
2
6941
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...
4
2681
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...
1
1873
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...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.