I have an ImageHandler class that I am using to retrieve photos from an active directory. However I want to display a default image in the event that one cannot be retrieved. I tried using return Bitmap.FromFile ( "~/images/default_person.jpg" );
however it appears as though that is looking for a file system path.
I would like to specify an image that is located in my solution in the folder "images" without hardcoding a system path into the code.
Here is the piece of code that is getting the image:
static Image GetUserPicture ( string userName )
{
DirectoryEntry myLdapConnection = SCDirectoryEntry.GetDirectoryEntry ( );
using ( DirectorySearcher dsSearcher = new DirectorySearcher ( myLdapConnection ) )
{
dsSearcher.Filter = String.Format ( "(SAMAccountName={0})", userName );
SearchResult result = dsSearcher.FindOne ( );
DirectoryEntry user = result.GetDirectoryEntry ( );
byte [ ] data = user.Properties [ "jpegPhoto" ].Value as byte [ ];
if ( data != null )
{
var s = new MemoryStream ( data );
return Bitmap.FromStream ( s );
}
//return default image here
return Bitmap.FromFile ( "~/images/default_person.jpg" );
}
}
You can use Server.MapPath( "~/images/default_person.jpg" );
Server.MapPath will convert this relative path into physical path as describe here, in MSDN page.