Subscribe to The Madman Scribblings        RSS Feed
-----

A Simple Image Cache

Icon 1 Comments
A Simple Image Cache

Suppose your application downloads images and those images don't change that often.
It would be better to keep a cache of those images, and then only download the images you need.



Images

<Serializable()>
Public Class Images
  Dim _TheImageCache As New Dictionary(Of String, Image)
  Dim _HasChanges As Boolean = False
  Dim _CachePath As Uri
  Public Sub New(CachePath As Uri, Optional AutoSave As Boolean = False)
    _CachePath = CachePath
    Me.AutoSave = AutoSave
    _HasChanges = False
  End Sub

  Public Property AutoSave As Boolean
  Public ReadOnly Property HasChanges() As Boolean
    Get
      Return _HasChanges
    End Get
  End Property

  Public Property CachePath As Uri
    Get
      Return _CachePath
    End Get
    Set(newCachePath As Uri)
      If _CachePath <> newCachePath Then
        _CachePath = newCachePath
        _HasChanges = True
      End If

    End Set
  End Property

  Default Public Property Images(ByVal imgKey As String) As Image
    Get
      If _TheImageCache.ContainsKey(imgKey) Then
        Return _TheImageCache(imgKey)
      Else
        Return Nothing
      End If
    End Get
    Set(value As Image)
      If _TheImageCache.ContainsKey(imgKey) Then
        _TheImageCache(imgKey) = value
      Else
        _TheImageCache.Add(imgKey, value)
      End If
      _HasChanges = True
      ShouldIDoAnAutoSave()
    End Set
  End Property

  Private Sub ShouldIDoAnAutoSave()
    If AutoSave Then Save()
  End Sub

  Public Function Delete(imgKey As String) As Boolean
    Dim SuccessfullyRemovedKey = _TheImageCache.Remove(imgKey)
    If SuccessfullyRemovedKey Then _HasChanges = True
    ShouldIDoAnAutoSave()
    Return SuccessfullyRemovedKey
  End Function

  Public Function Save() As Boolean
    If Not _HasChanges Then Return True
    Try
      _HasChanges = False
      Using fs As New IO.FileStream(Me.CachePath.OriginalString, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Write)
        Me.Serialise(fs)
      End Using
      Return True
    Catch
      _HasChanges = True
      Return False
    End Try
  End Function

  Public Shared Function CreateFromFile(filepath As Uri, Optional AutoSave As Boolean = False) As Images
    Dim imgs As Images = Nothing
    Try
      Using fs As New IO.FileStream(filepath.OriginalString, IO.FileMode.Open, IO.FileAccess.ReadWrite)
        imgs.Deserialise(fs)
      End Using
    Catch
      Return New Images(filepath, AutoSave)
    End Try
    Return If(imgs, New Images(filepath, AutoSave))
  End Function

End Class



Example Usage

Public Class Form1

  Dim imgs As Images
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    imgs = Images.CreateFromFile(New Uri("ci.i", UriKind.Relative))

    Dim xm = <weatherIconUrl>
               <![CDATA[http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png]]>
             </weatherIconUrl>
    Dim url = xm.Value.Trim
    If imgs(url) Is Nothing Then imgs(url) = Bitmap.FromStream((New Net.WebClient).OpenRead(url))

    Me.PictureBox1.Image = imgs(xm.Value.Trim)
    imgs.Save()
  End Sub
End Class



The Extension Methods
Spoiler

1 Comments On This Entry

Page 1 of 1

Michael26 Icon

12 March 2013 - 02:48 AM
Thanks for the example Adam.
0
Page 1 of 1

Search My Blog

Recent Entries

Recent Comments