Writing thumbnailPhoto property to Active Directory
<p>I'm creating an application (.Net 3.5) to allow users to update their own photos and telephone numbers in Active Directory.</p>
<p>I'm using the UserPrincipal class, which I've extended using this <a href="https://stackoverflow.com/questions/9540436/get-job-title-using-system-directoryservices-accountmanagement">example</a></p>
<pre><code>    // Create the &quot;thumbnailPhoto&quot; property.    
    [DirectoryProperty(&quot;thumbnailPhoto&quot;)]
    public byte[] thumbnailPhoto
    {
        get
        {
            if (ExtensionGet(&quot;thumbnailPhoto&quot;).Length != 1)
                return null;

            return (byte[])ExtensionGet(&quot;thumbnailPhoto&quot;)[0];
        }
        set
        {
            ExtensionSet(&quot;thumbnailPhoto&quot;, value);
        }
    }
</code></pre>
<p>I get the byte array and write it to a pictureBox with</p>
<pre><code>pictureBoxthumbnail.Image = Image.FromStream(new MemoryStream(userPrincipal.thumbnailPhoto));
</code></pre>
<p>This shows the picture, on the form, so far so good. When I try to write the image to Active Directory, I convert the pictureBox to a byte array with</p>
<pre><code>userPrincipal.thumbnailPhoto = ImageManipulation.imageToByteArray(pictureBoxthumbnail.Image);

public static byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    return ms.ToArray();
}
</code></pre>
<p>And try and save the data, but I get the following exception.</p>
<blockquote>
<p>System.DirectoryServices.AccountManagement.PrincipalOperationException was unhandled</p>
<p>Message=Unspecified error</p>
<p>Source=System.DirectoryServices.AccountManagement</p>
<p>ErrorCode=-2147467259</p>
</blockquote>
<p>I suspect my pictureBox to byte array is wrong. Is anyone able to help?</p>
<p>Thank you.</p>