Using the jpegPhoto attribute in AD – Part II

The previous post ( on this topic showed you how to import picture data into AD. This post shows you how to retieve the data and write it back out to a file. It might be more realistic to retrieve the data from AD and display it in a portal. I'm sure someone could adapt this code to do this quite easily.  If you know how, please post or email me.

Basically, this is the reverse of the previous code using System.DirectoryServices then System.IO. 

Here is the code:
Imports System.IO
Imports System.DirectoryServices

Module Module1
    Sub Main()
        Dim outFile As System.IO.FileStream
        Dim binaryData() As Byte
        Dim strFileName As String

        strFileName = "c:\PictureExportedFromAD.jpg"

        'Connect to AD
        Dim strDN As String = "CN=Joe User,OU=Employees,DC=company,DC=local"
        Dim strDCName As String = "DC-01"
        Dim myUser As New System.DirectoryServices.DirectoryEntry("LDAP://" & strDCName & "/" & strDN)

        'Retrieve picture into byte array
        Dim byteArray As Byte()
        byteArray = myUser.Properties("jpegPhoto").Value

        'Open file to export (delete if exists)
        If File.Exists(strFileName) Then
        End If

        outFile = New, FileMode.CreateNew, FileAccess.ReadWrite)

        'Loop through bytes and write to file
        Dim singleByte As Byte

        For Each singleByte In byteArray
    End Sub
End Module

Comments (4)

  1. Naomi Alpern says:

    Ok – I have been able to import a photo into this attibute, but I have a question.

    Is there anywhere that this image will display within the properties of AD? I can see the usefulness of having the attribute in a users properties, but how is it viewable?

    I understand that the code you have above will retrieve the info from the AD and write it out to a file, but what if you just want to view the data stored in that attribute without an output file?

    Any ideas on this would be great!!


  2. Good question Naomi. Two ways you might want to display this data. One is via a web page. I don’t have this code, but I bet it is not too hard.

    The other option is to display the picture in Active Directory Users & Computers. This can be done with "display specifiers" and extensions to ADUC. Look in MSDN and you will see information on how to add custom tabs to ADUC and display custom fields.

  3. Naomi Alpern says:

    Easier said then done.

    I am aware of the display specifiers, and I understand what they do. The issue I am having is that in order to add a tab or a field to display the JPEG for each user, it seems that a COM object must be written and it must be done in C++ according to the articles I have read. I dont have any issue with using ADSIEdit to mosify the display specifiers, but it just seems like there is way more to it then that.

    I wonder if you are aware of any tools that would make the process easier for someone like me who is not a programmer.


  4. I agree that this is not easy. Unfortunately, the only way to do this that I am aware of is creating the COM object. I did not think you needed C++ and there are samples, but there is no way to avoid the issue.

Skip to main content