0

Grabbing a thumbnailPhoto from active directory. Would this be the correct way of encoding it to byte array. Because when I try to convert this byte array to an image, I get parameter not valid error. So I believe it is the way it is being encoded. Note, I am retrieving byte information, but wondering if it is correct information.

foreach (SearchResult iResult in searchresult)
{
     ADUser userAttributes = new ADUser("", "", "", "", "", "", "", null);
     foreach (string PropertyName in iResult.Properties.PropertyNames)
     {
         foreach (Object key in iResult.GetDirectoryEntry().Properties[PropertyName])
         {
             try
             {

                 switch (PropertyName.ToUpper())
                 {
                     case "DISPLAYNAME":
                         DisplayName = key.ToString();
                         userAttributes.Name = DisplayName;
                         break;

                     case "SAMACCOUNTNAME":
                         SAMAccountName = key.ToString();
                         userAttributes.DomainUserName = SAMAccountName;
                         break;

                     case "MAIL":
                         Mail = key.ToString();
                         userAttributes.EmailAddress = Mail;
                         break;

                     case "DESCRIPTION":
                         Description = key.ToString();
                         userAttributes.JobDescription = Description;
                         break;

                     case "TELEPHONENUMBER":
                         TelephoneNumber = key.ToString();
                         userAttributes.TelephoneNumber = TelephoneNumber;
                         break;

                     case "FAX":
                         Fax = key.ToString();
                         userAttributes.FaxNumber = Fax;
                         break;

                     case "DEPARTMENT":
                         Department = key.ToString();
                         userAttributes.Department = Department;
                         break;

                     case "THUMBNAILPHOTO":
                         BinaryFormatter bf = new BinaryFormatter();
                         MemoryStream ms = new MemoryStream();
                         bf.Serialize(ms, key);

                         ThumbnailPhoto = ms.ToArray();
                         userAttributes.ThumbnailPhoto = ThumbnailPhoto;
                         break;
                    }
                 }
                 catch
                 {

                 }
              }
           }

         Users.Add(userAttributes);
       }

     return View(Users);
    }
  return View();
 }

In my View:

                    <td>
                        @if (item.ThumbnailPhoto != null)
                        {
                            Image thumbnail;
                            byte[] byteArray = item.ThumbnailPhoto;
                            using (MemoryStream s = new MemoryStream(byteArray, 0, byteArray.Length))
                           {

                               s.Write(byteArray, 0, item.ThumbnailPhoto.Length);
                               s.Position=0;
                               thumbnail = Image.FromStream(s,true);
                           }

                        }
                    </td>

If it is wrong, how would I encode this properly.

3
  • I think you should be resetting the position of the MemoryStream before reading from it. So you need to add "s.Position=0;" before thumbnail = Image.FromStream(.....) Commented Aug 21, 2014 at 16:32
  • another option would be s.Seek(0, SeekOrigin.Begin); Commented Aug 21, 2014 at 16:34
  • Sorry, through trying different things. I forgot to add that ive already tried that.
    – 1011 1110
    Commented Aug 21, 2014 at 17:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.