SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I suppose you could cache anything but why cache an image?

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code VBNET:
    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.

  4. #4
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,880
    Mentioned
    48 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,889
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    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/ap...ubdomains.html
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  6. #6
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    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.

  9. #9
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,889
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by dude9er View Post
    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.
    Can you give us the link to the page and no doubt there will be an abundance of posters offering how improvements.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  11. #11
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  12. #12
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Yes.


    In the master...


    <
    headrunat="server">
    <title>
    <asp:ContentPlaceHolderID="TitlePlaceholder"runat="server"/>
    </
    title>
    <asp:ContentPlaceHolderID="HeadPlaceholder"runat="server"/>
    </
    head>

    And then in your views...

    <asp:ContentID="HeadContent"ContentPlaceHolderID="HeadPlaceholder"runat="server">
    <!-- add metas here on the pages you want them -->
    </
    asp:Content>

  13. #13
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  14. #14
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    How big is this background image to actually effect bandwidth outlays in 2010?

  15. #15
    SitePoint Zealot chieftain's Avatar
    Join Date
    Feb 2005
    Location
    Michigan
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good question


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •