Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am reading the image from Active Directory the Property["thumbnailPhoto"].

My Model assigns it to this:

public Image thumbnailPhoto { get; set; }

My Service runs this code to put get the image from Active Directory and put it into my Model.

public Image GetUserPicture(string userName)
        {
            using (DirectoryEntry de = new DirectoryEntry("LDAP://server.com"))
            using (DirectorySearcher ds = new DirectorySearcher(de))
            {
                ds.Filter = ("(&(objectCategory=person)(objectClass=User)(sAMAccountName=" + userName + "))");
                ds.PropertiesToLoad.Add("thumbnailPhoto");
                SearchResult rs = ds.FindOne();
                using (MemoryStream s = new MemoryStream(rs.Properties["thumbnailPhoto"][0] as byte[]))
                {
                    return Bitmap.FromStream(s);
                }
            }
        }

I call the above method and assign it to the Model from this line:

user.thumbnailPhoto = GetUserPicture(rs.Properties["sAMAccountName"][0].ToString());

This all seems to be working, no errors, I can see in the debugger that user.thumbnailPhoto is of type System.Drawing.Bitmap.

Where I am stuck now is how to display this on a webpage?

Is it possible to throw this thumbnailPhoto into a Session["UserPhoto"] since I will need it on every single page?

share|improve this question
    
You will have to create helper action that returns content of the image and reference it in your model by some ID (userName?). –  Ilia G Jun 13 '13 at 15:28
    
"Where I am stuck now is how to display this on a webpage?" - Are you displaying the image, or a string that represents the image type, like "bitmap" or "jpg"? –  mbeckish Jun 13 '13 at 15:29
    
Well when I throw it into a Session and then replace my blank image with the session. It just shows System.Drawing.Bitmap on the webpage. @mbeckish –  James Wilson Jun 13 '13 at 15:33

1 Answer 1

up vote 2 down vote accepted

I would convert it to a Base64 string then use that as the source.

   using (MemoryStream s = new MemoryStream(rs.Properties["thumbnailPhoto"][0] as byte[]))
   {
       byte[] imageBytes = s.ToArray();
       string base64String = Convert.ToBase64String(imageBytes);
   }


   <img src="image/png;base64,@Model.ImageBase64String" />
share|improve this answer
    
I tried this but it doesn't seem to be displaying the image. It's just a broken image box. Here is the HTML it creates. It creates a massivly long <img> tag however. The only difference is I am putting the image into a session variable. –  James Wilson Jun 13 '13 at 15:46
    
The Base64 string will be very long indeed. –  Gabe Jun 13 '13 at 16:12
    
Make sure the mime is correct, if its not a png, use the correct type... –  Gabe Jun 13 '13 at 16:13
    
Yep they were Jpeg's fixed the issue. Thanks! –  James Wilson Jun 13 '13 at 16:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.