Programmatically Cache Image

Is it possible to programmatically cache an image using VB code behind? If so, can someone lead me in the right direction?

THANKS

I suppose you could cache anything but why cache an image?

I am using a masterpage and therefore the meta tag is in the header of that page and affects everything. Any way around this?

I really want to cut back on bandwidth for obvious reasons.

Any feedback would be great.

Wow. I totally missunderstood that one from the beginning. Heh.

And those headers are extremely effective. I don’t think there’s really a way around it. If you use those, they affect everything.

I tend not to use them and let the user set caching options in their browser so everybody gets what they want.

Correct me if I’m wrong OP, but it seems like your talking about the page bg, which you’e most likely set using css (inline or rel, doesn’t matter).

What you might want to do is identify which pages in your app display static only, and those that display dynamic, then simply include the no-cache headers only on the dynamic pages. I believe you can use the Page object to access the meta tags, though I’m not certain, if you want to add them via code.

It’s not a perfect, or elegent solution, but it will cut down on some of your bandwidth usage.

I am using two meta tags
<meta http-equiv=“cache-control” content=“no-cache” />
<meta http-equiv=“Pragma” content=“no-cache” />

because the site loads data to run, this works to keep the data current for the user, but I want to be able to cache the background image of the site so they are not having to download it each time.

Try using Session or HttpContext.Items collections (depending on how long you need to cache it). Also, try not to use them directly. Instead, write some session extension methods so you can set/get them in a typed fashion.

Imports System.Web
 
Namespace Sample.FooBar
 
 Public NotInheritable Class SessionExtensions

  Private Sub New()
  End Sub
 
  <System.Runtime.CompilerServices.Extension> _
  Public Shared Sub SetImage(session As HttpSessionStateBase, key As String, image As Byte())
   session("Image:" & key) = image
  End Sub
 
  <System.Runtime.CompilerServices.Extension> _
  Public Shared Function GetImage(session As HttpSessionStateBase, key As String) As Byte()
   Return TryCast(session("Image:" & key), Byte())
  End Function
 
 End Class
 
End Namespace

Then simply import the namespace into your page class and call it like this:

Session.SetImage(“MyImage”, data);

and

byte data = Session.GetImage(“MyImage”);

This is, of course, assuming your storing it as a byte array. Alter the code otherwise.

Good question

How big is this background image to actually effect bandwidth outlays in 2010?

Yes.

In the master…


<
head runat=“server”>
<title>
<asp:ContentPlaceHolder ID=“TitlePlaceholder” runat=“server” />
</
title>
<asp:ContentPlaceHolder ID=“HeadPlaceholder” runat=“server” />
</
head>

And then in your views…

<asp:Content ID=“HeadContent” ContentPlaceHolderID=“HeadPlaceholder” runat=“server”>
<!-- add metas here on the pages you want them –>
</
asp:Content>

I ended up creating two master pages one with the no-cache metas for the data pages and one without. Thanks for everyone’s help.

Can you give us the link to the page and no doubt there will be an abundance of posters offering how improvements.

I have recently moved my thumbnails on a site to a sub-domain on the same site and the page loading appears significantly faster with the images popping up as and when they are loaded.

Check out this link: http://www.askapache.com/htaccess/apache-speed-subdomains.html

How do you want to cache the image? The bytes or what? It will not really make sense, as the user has to still download the file. Cache would need to be client side. Unless you are talking about a dynamic thumbnailer and you want to cache the output.

Just remember, caching images in this manner will not really help, as your site will tend to have more images than memory available for caching. Best is to use a combination of Memory and disk caching

You could try sending the headers for individual files instead of doing the page.

I don’t know how it would be done in .NET but it seems there should be some way to do it.