Answer on Question #38248 – Programming – Visual Basic
The task of displaying Windows user account picture in the Picturebox consists of given steps:
1. Getting current user info
2. Getting Windows folder location
3. Forming path to the user picture (DRIVE\USER\appData\local\Temp\USER.bmp)
4. Setting Picturebox ImageLocation property to account picture path.
Code below uses the windows form with Picturebox control named PictureBox1. The code is bound to Picturebox1 Click event. Please note that provided code works correctly only under Win7, other OS versions have different user picture locations.
namespace for windows security options, including users list
Imports System.Security.Principal
Public Class Form1
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
'creating windows identity object
Dim UserIdentityInfo As WindowsIdentity
Dim strUser() As String
Dim strtemp() As String
Dim strPath As String
'getting current user info
UserIdentityInfo = WindowsIdentity.GetCurrent()
'the name property is in PCNAME\USERNAME format. So creating array that consists of PCNAME, and USERNAME, to get later second part after \
strUser = Split(UserIdentityInfo.Name.ToString(), "\")
'getting information about windows directory location. Drive name is needed only. creating array, splitted by \ character. only first entry is needed.
strtemp = Split(Environment.GetEnvironmentVariable("WINDIR").ToString, "\")
'forming the path to current user picture: DRIVE\USER\appData\local\Temp\USER.bmp
strPath = strtemp(0) & "\Users" & strUser(1) & "\AppData\Local\Temp" & strUser(1) & ".bmp"
'telling picturebox to fit size of the picture.
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
'setting picturebox source to generated path to account picture.
PictureBox1.ImageLocation = strPath
End Sub
End Class
Comments