PC Review


Reply
Thread Tools Rate Thread

Avoiding Image Caching

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      13th Aug 2005
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.NoCache)

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
(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      13th Aug 2005
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(500))
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


 
Reply With Quote
 
Chris Botha
Guest
Posts: n/a
 
      13th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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.NoCache)
>
> 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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
>



 
Reply With Quote
 
W.G. Ryan MVP
Guest
Posts: n/a
 
      13th Aug 2005
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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(500))
> 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
>



 
Reply With Quote
 
laimis
Guest
Posts: n/a
 
      13th Aug 2005
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
 
Reply With Quote
 
Joerg Jooss
Guest
Posts: n/a
 
      17th Aug 2005
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:news-(E-Mail Removed)
 
Reply With Quote
 
Ben Strackany
Guest
Posts: n/a
 
      17th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.NoCache)
>
> 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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      18th Aug 2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET 2.0 image caching Amelyan Microsoft ASP .NET 4 1st Feb 2006 06:42 PM
Avoiding Image Caching Nathan Sokalski Microsoft Dot NET 7 18th Aug 2005 07:57 AM
Re: Avoiding Image Caching tom pester Microsoft ASP .NET 0 13th Aug 2005 07:19 PM
Image control CACHING IMAGE Suraj Joneja Microsoft ASP .NET 6 7th Feb 2005 09:11 AM
IE Image Caching Jeremy Levy Windows XP Internet Explorer 0 25th Aug 2003 10:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:08 PM.