Results 1 to 9 of 9

Thread: [RESOLVED] Caching thumbnails

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Resolved [RESOLVED] Caching thumbnails

    Hi,

    I'm using an asp.net page to dynamically create thumbnails, and I was thinking I should try and cache them to load them a bit faster. I've never tried using the Cache object before, but I'm sure it should work like I'm trying to use it. Anyway, here's the code for caching and retrieving the thumbnail:
    VB Code:
    1. Dim bmp As Bitmap = Cache.Get(strPath & "_" & sWidth & "_" & sHeight)
    2.  
    3.   If bmp Is Nothing Then
    4.     bmp = CreateThumbnail(strPath, sWidth, sHeight)
    5.     Cache.Insert(strPath & "_" & sWidth & "_" & sHeight, _
    6.      bmp, Nothing, DateTime.MaxValue, TimeSpan.Zero)
    7.   End If
    8.   If bmp Is String.Empty Then Return

    strPath is the location of the photo, sWidth the width and sHeight the height. The thumbnail is then saved to the output stream. Unfortunately, this doesn't seem to work.

    Any ideas?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Caching thumbnails

    If the page itself is serving out the images (dynamic images), then add an OutputCache directive to the page:

    <%@ OutputCache duration="10000" varybyparam="ImageID" %>

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Caching thumbnails

    Does that mean I'll need to create an ImageID variable? Something along the lines of the 'strPath & "_" & sWidth & "_" & sHeight' I've used for the Cache key? What kind of scope does this need to have?

    Oh - and the error I was getting was 'invalid parameter' in the Bitmap.Save() call. It must be something to do with what gets stored in the Cache object.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Caching thumbnails

    What I mean is, when the image is requested, the request will look like this:

    myimage.aspx?ImageID=28391

    All you should do in the page is retrieve it from the DB. The next time the image is requested, the cache object will serve it automatically.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Caching thumbnails

    The image is requested by filename - they're not stored in a database. The resize script also accepts different parameters for the width and height. Does whatever I use as my 'varybyparam' need to be in the QueryString?

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Caching thumbnails

    But you're still using a myimage.aspx page right?

    Yes, whatever you use as your varybyparam must be in the querystring, else it defeats the purpose of caching it.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Caching thumbnails

    Well, I somehow missed that last post of yours - failed to get the email notification. Anyway, here's what I ended up doing:

    VB Code:
    1. <%@ Page Language="VB" Debug="False" Trace="False" %>
    2. <%@Import Namespace="System.Drawing" %>
    3. <%@Import Namespace="System.Drawing.Imaging" %>
    4. <%@Import Namespace="System.Drawing.Drawing2D" %>
    5.  
    6. <script language="VB" runat="server">
    7. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.   Dim strImage As String = Replace(Request.QueryString("image"), "%20", " ")
    9.   Dim strAlbum as String = Replace(Request.QueryString("album"), "%20", " ")
    10.  
    11.   If strImage = String.Empty Or strAlbum = String.Empty Then Exit Sub
    12.   If InStr(strImage, "/") <> 0 Or InStr(strImage, "\") <> 0 Or _
    13.    InStr(strAlbum, "/") <> 0 Or InStr(strAlbum, "\") <> 0 Then
    14.     Exit Sub
    15.   End If
    16.  
    17.   Dim sWidth = Request("w")
    18.   Dim sHeight = Request("h")
    19.  
    20.   If Not sHeight = String.Empty And Not sWidth = String.Empty Then
    21.     sWidth = Integer.Parse(sWidth)
    22.     sHeight = Integer.Parse(sHeight)
    23.   Else
    24.     sWidth = 20
    25.     sHeight = 20
    26.   End If
    27.  
    28.   Dim strPath As String = Server.MapPath("photos/" & strAlbum & "/" & strImage)
    29.   Dim fsMain As System.IO.FileStream = New System.IO.FileStream(strPath, _
    30.    System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read)
    31.  
    32.   'Cache.Remove(strPath & "_" & sWidth & "_" & sHeight)
    33.   Dim bytImage As Byte() = Cache.Get(strPath & "_" & sWidth & "_" & sHeight)
    34.  
    35.   Dim msTempStream As System.IO.MemoryStream
    36.   Dim imgMain As System.Drawing.Image
    37.  
    38.   If bytImage Is Nothing Then
    39.     ReDim bytImage(fsMain.Length)
    40.     fsMain.Read(bytImage, 0, fsMain.Length)
    41.     fsMain.Close()
    42.  
    43.     msTempStream = New System.IO.MemoryStream(bytImage)
    44.     imgMain = System.Drawing.Image.FromStream(msTempStream)
    45.  
    46.     Dim lnRatio As Decimal
    47.     Dim lnNewWidth As Integer = 0
    48.     Dim lnNewHeight As Integer = 0
    49.     If (imgMain.Width > imgMain.Height) Then
    50.       lnRatio = Integer.Parse(sWidth) / imgMain.Width
    51.       lnNewWidth = Integer.Parse(sWidth)
    52.       Dim lnTemp As Decimal = imgMain.Height * lnRatio
    53.       lnNewHeight = lnTemp
    54.     Else
    55.       lnRatio = Integer.Parse(sHeight) / imgMain.Height
    56.       lnNewHeight = Integer.Parse(sHeight)
    57.       Dim lnTemp As Decimal = imgMain.Width * lnRatio
    58.       lnNewWidth = lnTemp
    59.     End If
    60.  
    61.     Dim imgThumbnail As System.Drawing.Image = New Bitmap(lnNewWidth, lnNewHeight)
    62.     Dim graThumbnail As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(imgThumbnail)
    63.  
    64.     graThumbnail.InterpolationMode = InterpolationMode.HighQualityBicubic
    65.     graThumbnail.SmoothingMode = SmoothingMode.HighQuality
    66.     graThumbnail.PixelOffsetMode = PixelOffsetMode.HighQuality
    67.     graThumbnail.CompositingQuality = CompositingQuality.HighQuality
    68.     graThumbnail.DrawImage(imgMain, 0, 0, lnNewWidth, lnNewHeight)
    69.  
    70.     Dim iciInfo() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
    71.     Dim encpMain As EncoderParameters = New EncoderParameters(1)
    72.     encpMain.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L)
    73.  
    74.     Dim strOutputFilename As String = String.Empty
    75.     strOutputFilename = Request.QueryString("OutputFilename")
    76.  
    77.     If Not strOutputFilename = String.Empty Then
    78.       Try
    79.         imgThumbnail.Save(Server.MapPath("photos/" & strAlbum & "/") & strOutputFilename, ImageFormat.Jpeg)
    80.       Catch ex As Exception
    81.         imgThumbnail.Dispose()
    82.         Return
    83.       End Try
    84.     End If
    85.  
    86.     Response.ContentType = "image/jpeg"
    87.     Dim msSaveStream As System.IO.MemoryStream = New System.IO.MemoryStream
    88.     imgThumbnail.Save(msSaveStream, iciInfo(1), encpMain)
    89.     msSaveStream.WriteTo(Response.OutputStream)
    90.     Dim bytSave(msSaveStream.Length) As Byte
    91.     msSaveStream.Position = 0
    92.     msSaveStream.Read(bytSave, 0, msSaveStream.Length)
    93.     Cache.Insert(strPath & "_" & sWidth & "_" & sHeight, _
    94.      bytSave, Nothing, DateTime.MaxValue, TimeSpan.Zero)
    95.     imgThumbnail.Dispose()
    96.   Else
    97.     msTempStream = New System.IO.MemoryStream(bytImage)
    98.     Response.ContentType = "image/jpeg"
    99.     msTempStream.WriteTo(Response.OutputStream)
    100.  
    101.     Dim strOutputFilename As String = String.Empty
    102.     strOutputFilename = Request.QueryString("OutputFilename")
    103.  
    104.     If Not strOutputFilename = String.Empty Then
    105.       Try
    106.         imgMain = System.Drawing.Image.FromStream(msTempStream)
    107.         imgMain.Save(Server.MapPath("photos/" & strAlbum & "/") & strOutputFilename, ImageFormat.Jpeg)
    108.       Catch ex As Exception
    109.         imgMain.Dispose()
    110.         Return
    111.       End Try
    112.       imgMain.Dispose()
    113.     End If
    114.   End If
    115. End Sub
    116. </script>

    There's a distinct lack of comments, but essentially, instead of caching the Image object, I cache a byte array containing the image.

    What do you reckon? Is there going to be a big difference in speed?

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Caching thumbnails

    Not a big difference, just a tiny one when it comes to the point where it attempts to query the cache to retrieve the image before it then proceeds to read the image from disk.

    If you expect the page to be heavily used then you might want to consider what I suggested. Else you can stick to what you have now, it's fine, good work.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Caching thumbnails

    Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width