Retrieve members avatar

Guides, Help & Support, Tutorials and Examples on how to access and use the Site API.
1 post Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Retrieve members avatar
CodenStuff
Hello,

This is a quick function to help get a members avatar image when displaying things like status messages and friends.

It checks to see if an avatar image already exits in a local folder and if not it downloads and saves the avatar as a thumbnail into the folder. If the avatar already exists then it uses the image from the local folder which helps display content faster because you dont need to load the images from the web all the time.

Firstly you will want to check/create the folder to store the avatars in:
Code: Select all
If Not My.Computer.FileSystem.DirectoryExists(Application.StartupPath & "\Avatars\") Then
            My.Computer.FileSystem.CreateDirectory(Application.StartupPath & "\Avatars\")
        End If
Then the function:
Code: Select all
    Public Function AvatarIMG(ByVal Avatar As String, ByVal Username As String)
        If Avatar Is Nothing Or Username Is Nothing Then
            Return Nothing
        Else
            If Not My.Computer.FileSystem.FileExists(Application.StartupPath & "\Avatars\" & Username & ".png") Then
                Using WebC As New Net.WebClient()
                    Dim Thumb = Image.FromStream(WebC.OpenRead(Avatar.Replace("./download/file.php", "http://www.codenstuff.com/forum/download/file.php")))
                    Thumb.GetThumbnailImage(45, 45, Function() False, Nothing).Save(Application.StartupPath & "\Avatars\" & Username & ".png")
                    Thumb.Dispose()
                    Return Application.StartupPath & "\Avatars\" & Username & ".png"
                End Using
            Else
                Return Application.StartupPath & "\Avatars\" & Username & ".png"
            End If
        End If
    End Function
Then to use it you just pass in the avatar url and members username:
Code: Select all
AvatarIMG(item.avatar, item.name)
Example loading into a picturebox:
Code: Select all
Picturebox.LoadAsync(AvatarIMG(item.avatar, item.name))
Works well enough cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
1 post Page 1 of 1
Return to “Codenstuff API”