在C#中检索thumbnailPhoto

时间:2014-08-21 15:14:23

标签: c# asp.net bytearray memorystream

从活动目录中抓取thumbnailPhoto。这是将其编码为字节数组的正确方法吗?因为当我尝试将此字节数组转换为图像时,出现parameter not valid错误。所以我相信它是被编码的方式。注意,我正在检索字节信息,但想知道它是否是正确的信息。

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();
 }

在我的观点中:

                    <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>

如果错误,我将如何正确编码。

0 个答案:

没有答案